我有一个表Users
,其中包含以下列:
$users = User::select(['id','name','level','parent','updated_at'])
->where('level','>',1)
->get();
输出:
id name level parent updated_at
----+---------+-----------+--------+-------------
12 Jhon 1 0 2016-02-01
99 Carl 2 12 2016-02-01
然后:我尝试使用显示列parent
如何关联名称id
,例如,在第二行,显示Jhon
替换12
。
我尝试了这段代码......任何阅读帮助...
$users = User::select(['id','name','level',
DB::raw("SELECT * FROM users WHERE id = users.parent)
'updated_at'])
->where('level','>',1)
->get();
预期产出:
id name level parent updated_at
----+---------+-----------+--------+-------------
12 Jhon 1 2016-02-01
99 Carl 2 Jhon 2016-02-01
答案 0 :(得分:0)
您希望leftJoin
获取父名称:
$users = User::select(['users.id','users.name','users.level', 'parent.name')
->leftJoin('users AS parent','users.parent','=','parent.id')
->where('users.level','>',1)
->get();