Laravel急切加载,加载表字段


Laravel eager loading, loaded table fields

我如何选择我想从with ORM中获得哪些字段?例如

$tourTeams = Tournament::with('teams')->where('id', $tourId)->first();

从团队关系中,我只想获得名称(没有id和时间戳)。

我在文档中没有写。对于Tournament雄辩,我可以通过get函数来实现,同时传递一个字段名称数组,如:get(array('name', 'id'))。但是我怎么在Team上做到这一点呢?

注意:这里是如何团队相关的锦标赛,这段代码取自锦标赛雄辩文件:

公共职能团队(){return $this->belongsToMany('Team', ' tournament ');}

您可以像这样从关系中获得特定的列:

 $tourTeams = Tournament::with(['teams'=>function($q){
         $q->select('id','name');
 }])->where('id', $tourId)->first();