我有以下关系
资产有多种规模。 大小有一个资产。
我得到的数据如下:
$data = Asset::with('size')->where('active', 1)->orderBy('updated_at', 'DESC')->get();
如何限制通过上述查询返回的数据?以下限制资产数据:
$data = Asset::with('size')->where('active', 1)->orderBy('updated_at', 'DESC')->get(array('id', 'name');
但我需要限制来自大小的数据。
我在尺寸模型中尝试了以下内容,但没有运气:
return $this->belongsTo('Asset')->select(array('asset_id', 'name'));
答案 0 :(得分:1)
您可以为预先加载指定条件:
$data = Asset::with(array('size'=> function($query)
{
$query->select('id','name','asset_id'); // asset_id is needed for mapping
}))->where('active', 1)->orderBy('updated_at', 'DESC')->get();