$select = Model::with(['relation' => function($query) {
$query->select('id', 'somefield');
}])->get();
使用此代码时,我的Model
对象的relation
字段包含somefield
。
{
... some Model fields
...
relation => {
id => someid
somefield => somevalue
}
}
但是如果我需要somefield
代替Model
字段呢?
{
... some Model fields
...
somefield => somevalue
}
编辑:换句话说,我希望使用Eloquent
获得与后续查询相同的结果\DB::table('table1')
->select('table1.field1', 'table2.field2 as otherfield',
->leftJoin('table2', 'table2.id', '=', 'table1.parent_id')->get();
答案 0 :(得分:0)
像这样使用Laravel plcuk方法:
$shopAddresses = Shop::where('name', 'Starbucks')->pluck('address');