Php Laravel雄辩地讲述了如何选择关系字段作为主要对象的某些字段
当使用此代码时,我得到了我的Php Laravel雄辩地讲述了如何选择关系字段作为主要对象的某些字段,php,laravel,select,laravel-4,eloquent,Php,Laravel,Select,Laravel 4,Eloquent,当使用此代码时,我得到了我的模型对象和关系字段,该字段包含somefield $select = Model::with(['relation' => function($query) { $query->select('id', 'somefield'); }])->get(); 但是如果我需要somefield作为Model的字段,该怎么办 { ... some Model fields ... relation => {
模型
对象和关系
字段,该字段包含somefield
$select = Model::with(['relation' => function($query) {
$query->select('id', 'somefield');
}])->get();
但是如果我需要somefield
作为Model
的字段,该怎么办
{
... some Model fields
...
relation => {
id => someid
somefield => somevalue
}
}
编辑:换句话说,我想得到和下面的查询相同的结果,使用雄辩的语言
{
... some Model fields
...
somefield => somevalue
}
使用Laravel plcuk方法,如下所示:
\DB::table('table1')
->select('table1.field1', 'table2.field2 as otherfield',
->leftJoin('table2', 'table2.id', '=', 'table1.parent_id')->get();
你能做model.somefield吗?@Anoxy我想能够调用model->somefield,而不是model->relation->somefield,我只需要使用laravel pulk函数,例如:$shopAddresses=Shop::where('name','Starbucks')->pulk('address');您应该能够执行->pull('address',joinfield.address)
$shopAddresses = Shop::where('name', 'Starbucks')->pluck('address');