Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel雄辩地讲述了如何选择关系字段作为主要对象的某些字段_Php_Laravel_Select_Laravel 4_Eloquent - Fatal编程技术网

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');