Laravel Eloquent如何选择关系字段作为主要对象的某些字段

时间:2016-02-11 04:38:23

标签: php laravel select laravel-4 eloquent

$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();

1 个答案:

答案 0 :(得分:0)

像这样使用Laravel plcuk方法:

$shopAddresses = Shop::where('name', 'Starbucks')->pluck('address');