[英]Laravel join same table for 2 columns
I do currently have this code: 我目前有这个代码:
return Datatable::query($query = DB::table('acquisitions')
->where('acquisitions.deleted_at', '=', null)
->where('acquisitions.status', '!=', 2)
->join('contacts', 'acquisitions.contact_id', '=', 'contacts.id')
->join('user', 'acquisitions.user_id', '=', 'user.id')
->select('contacts.*', 'acquisitions.*', 'acquisitions.id as acquisitions_id', 'user.first_name as supervisor_first_name', 'user.last_name as supervisor_last_name', 'user.id as user_id'))
The data from the user table is used for 2 columns: acquisitions.supervisor_id
and acquisitions.user_id
. 来自用户表的数据用于2列:
acquisitions.supervisor_id
和acquisitions.user_id
。 I need the first_name
and the last_name
for both of this tables, the above query does however currently only use the id
from the acquisitions.user_id
field. 我需要这两个表的
first_name
和last_name
,但上面的查询当前只使用acquisitions.user_id
字段中的id
。 I also tried to use a table alias, that does also not work, I assume that I'm doing something wrong here. 我也尝试使用表别名,这也行不通,我假设我在这里做错了。
So in short: I also need that the query selects the data for the user, based on the id from the acquisitions.supervisor_id
and makes it available as supervisor_first_name
and supervisor_last_name
. 因此,在短期:我还需要在查询选择基于来自id为用户的数据,
acquisitions.supervisor_id
并使其可作为supervisor_first_name
和supervisor_last_name
。
According to your next to last comment on the other answer, you need a self join per reference table. 根据你对另一个答案的最后评论,你需要每个参考表自我加入。 Try this:
尝试这个:
$result = DB::select('SELECT
u.name user_first_name,
u.last_name user_last_name,
u.email user_email,
s.name supervisor_name,
s.last_name supervisor_last_name,
s.email supervisor_email
FROM acquisitions a
JOIN users u ON a.user_id = u.id
JOIN users s ON a.supervisor_id = s.id');
return $result;
Note that $result
is an array of StdClass
objects, not a Collection, but you can still iterate it and call the current item's values: 请注意,
$result
是一个StdClass
对象数组,而不是Collection,但您仍然可以迭代它并调用当前项的值:
foreach ($result as $item) {
print($item->supervisor_first_name);
}
If you need a WHERE
clause, eg to get a specific user's row from acquisitions
, you would do that by adding a parameter to the query like so: 如果您需要
WHERE
子句,例如从acquisitions
特定用户的行,您可以通过向查询添加参数来实现,如下所示:
$result = DB::select('SELECT
u.name user_first_name,
u.last_name user_last_name,
u.email user_email,
s.name supervisor_name,
s.last_name supervisor_last_name,
s.email supervisor_email
FROM acquisitions a
JOIN users u ON a.user_id = u.id
JOIN users s ON a.supervisor_id = s.id
WHERE a.user_id = ?
', [3]);
EDIT 编辑
If you need the resultset to be a Collection
, you can easily convert the array to one, using the hydrate
method: 如果您需要将结果集作为
Collection
,则可以使用hydrate
方法轻松地将数组转换为1:
$userdata = \App\User::hydrate($result); // $userdata is now a collection of models
It should be something like; 应该是这样的;
return Datatable::query($query = DB::table('acquisitions')
->where('acquisitions.deleted_at', '=', null)
->where('acquisitions.status', '!=', 2)
->join('contacts', 'acquisitions.contact_id', '=', 'contacts.id')
->join('user', 'acquisitions.user_id', '=', 'user.id')
->select( \DB::raw(" contacts.*, acquisitions.*, acquisitions.id as acquisitions_id, user.first_name as supervisor_first_name, user.last_name as supervisor_last_name, user.id as user_id ") )
);
$devices = DB::table('devices as d')
->leftJoin('users as au', 'd.assigned_user_id', '=', 'au.id')
->leftJoin('users as cu', 'd.completed_by_user_id', '=', 'cu.id')
->select('d.id','au.name as assigned_user_name','cu.name as completed_by_user_name');
Also follow this link 也请点击此链接
https://github.com/yajra/laravel-datatables/issues/161 https://github.com/yajra/laravel-datatables/issues/161
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.