此连接的等效laravel关系查询

时间:2017-04-01 14:24:06

标签: laravel eloquent custom-fields

这是我的查询

$personnel_info = \DB::table('assigns AS a')
        ->join('boxes AS b','b.id','=', 'a.box_id')
        ->join('positions AS p','p.id','=', 'b.position_id')
        ->select('a.id','b.id AS box_id','p.id as position_id','p.title','a.status','a.end_date')
        ->where('a.personnel_id','=',$personnel_id)
        ->get();

和箱子的这种关系:

    class Boxes extends Model
{
    public function position()
    {
        return $this->belongsTo('Positions');
    }

    public function assign()
    {
        return $this->hasOne('Assigns', 'box_id');
    }
}

如何使用eloquent查询(也是realtionship)来替换DB facade查询? 我想为table.without在框模型中定义文件选择一些字段 TNX

1 个答案:

答案 0 :(得分:0)

尝试

$personnel_info = Assign::with('box.position')
->where('personnel_id', $personnel_id)
->get();

然后dd($personnel_info)查看返回的所有内容。如果您不喜欢这些值,请添加select()子句。