2016-04-03 10 views
0

我有一个API,它需要很长时间才能获得所有的信息,因为我只隐藏一些数据,但我想忽略不隐藏。我发现select()方法来选择极好的数据发送并减少查询我真正需要的所有信息的时间。 我试着刚的关系就这样以后使用select,只是从OPR_User表中检索唯一的名字:如何使用“选择”方法来减少使用Eager时的数据传输加载

public function creatorUser() { 
    return $this->belongsTo('Knotion\OPR_User', 'idCreatorUser', 'idUser')->select('name'); 
} 

,但不工作 这是我的型号代码

<?php 

namespace Knotion; 

use Illuminate\Database\Eloquent\Model; 

class CTL_Resource extends Model { 

    protected $table = "CTL_Resource"; 
    protected $primaryKey = "idResource"; 

    public $incrementing = false; 
    public $timestamps = false; 
    public static $snakeAttributes = false; 

    protected $hidden = [ 
     'coachVisibility', 'thumbnail', 
     'studentVisibility', 'isHTML','studentIndex', 'coachIndex', 
     'isURL', 'source', 'path', 'status', 'updateTime', 'isfolder', 
     'parentResource', 'idModifierUser', 'idResourceType', 'idCreatorUser', 'idCreationCountry' 
    ]; 

    protected $fillable = ['idResourceType','productionKey', 'idCreatorUser', 'idModifierUser', 'idCreationCountry', 'title', 'description', 'URL', 'fileName', 'extension', 'minimumAge', 'maximumAge', 'productionKey']; 

    public function creatorUser() { 
     return $this->belongsTo('Knotion\OPR_User', 'idCreatorUser', 'idUser'); 
    } 
    public function creationCountry() { 
     return $this->belongsTo('Knotion\CTL_Country', 'idCreationCountry', 'idCountry'); 
    } 
    public function resourceType() { 
     return $this->belongsTo('Knotion\CTL_ResourceType', 'idResourceType', 'idResourceType'); 
    } 
    public function quickTags() { 
     return $this->belongsToMany('Knotion\CTL_QuickTag', 'CTL_Resource_has_QuickTags', 'idResource','idQuickTag'); 
    } 
    public function tags() { 
     return $this->belongsToMany('Knotion\CTL_Tag','CTL_Resource_has_Tags', 'idResource', 'idTag'); 
    } 
    public function relatedTo() { 
     return $this->belongsToMany('Knotion\CTL_RelatedTo', 'CTL_Resource_has_RelatedTo', 'idResource', 'idRelatedTo'); 
    } 

} 

这是我的关系模型代码(仅在需要的情况下):

<?php 

namespace Knotion; 

use Illuminate\Database\Eloquent\Model; 

class OPR_User extends Model { 
    protected $table = "OPR_User"; 
    protected $primaryKey = "idUser"; 

    public $incrementing = false; 
    public $timestamps = false; 
    public static $snakeAttributes = false; 

    protected $hidden = ['firstName', 'secondName', 'firstSurName', 'secondSurName', 'password', 'picture', 'status', 'createTime', 'updateTime', 'idUserType', 'email']; 

    public function resources() { 
    return $this->hasMany('Knotion\CTL_Resource', 'idResource'); 
    } 
    public function userType() { 
    return $this->belongsTo('Knotion\CTL_UserType', 'idUserType', 'idUserType'); 
    } 

} 

这是我的控制器代码:

public function index(Request $request) { 

    $resources = CTL_Resource::all(); 

    $resources->resourceType->select('name'); 

     return $resources->load('creatorUser', 'creationCountry', 'resourceType', 'tags', 'quickTags', 'relatedTo'); 
    } 

回答

0

当您添加 - >选择后 - >属于关联它不再是一个实际的关系类型,它是一个查询生成器。您需要在调用 - > load之前添加选择。

+0

嘿,感谢您的评论。我不得不包括身份证工作,我没有发布答案:) –

0

要解决我必须包括ID也关系中,像这样的问题:

public function resources() { 
    return $this->hasMany('Knotion\CTL_Resource', 'idResource')->select('idResource', 'name'); 
    } 
相关问题