Laravel join same table for 2 columns - php

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. 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. 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.

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:
foreach ($result as $item) {
print($item->supervisor_first_name);
}
If you need a WHERE clause, e.g. to get a specific user's row from acquisitions, you would do that by adding a parameter to the query like so:
$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:
$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

Related

How to make sql nested/sub query in PHP

I'm struggling with implementing SQL query into my php code.
The query:
select template_id, min(created_at) as created_at from (
select distinct
root_template_id as template_id,
date_created as created_at,
response_id as response_id
from db.score
inner join db.users ON db.score.supplier_id = db.users.old_id
inner join db.acc ON db.acc.user_id = db.users.id
where db.acc.account_id = 25) as T
group by template_id
I was able to prepare only this part:
$query = DB::table('score')
->selectRaw('DISTINCT root_template_id as template_id, date_created as created_at, response_id')
->join('users', 'score.supplier_id', '=', 'users.old_id')
->join('acc', 'acc.user_id', '=', 'users.id')
->whereIn('acc.account_id', $request->id)
And it works, but it is only responsible for the nested part, for the subquery if I can call it like this. Can someone share any thoughts?
DB::table() usually receive table name as a string, but it can also receive Closure or an Illuminate\Database\Query\Builder instance, so this should do the trick for Laravel's select from subquery:
DB::table(function ($query) use ($request) {
$query->selectRaw('DISTINCT root_template_id as template_id, date_created as created_at, response_id')
->from('score')
->join('users', 'score.supplier_id', '=', 'users.old_id')
->join('acc', 'acc.user_id', '=', 'users.id')
->whereIn('acc.account_id', $request->id)
}, 'T')
->select([
'template_id',
DB::raw("MIN(created_at) AS created_at"),
])
->groupBy('template_id')
->get();

codeigniter join from 2nd table id with 3rd table id multi rows

I want to join multiple tables, as in my picture:
Here is my code:
$this->db->select('
pt2.turl as `p_img`,
p.title as `p_title`,
p.text as `p_text`,
p.create as `p_date`,
pt3.turl as `c_img`,
u.name as `c_name`,
c.text as `c_text`,
c.create as `c_date`
');
$this->db->from('posts as p, users as u, photos as pt2, photos as pt3');
$this->db->join('comments as c', 'p.id=c.pid AND u.id=c.uid');
$this->db->join('posts as p2', 'p2.pid=pt2.id', 'rihgt');
$this->db->join('users as u2', 'u2.photoid=pt3.id', 'right');
$this->db->order_by('c.id', 'DESC');
$this->db->limit('7');
$qry = $this->db->get();
return $qry->result();
If I understand you correctly, this is kind of what you're looking for. I haven't tried it out so it may not be exact, but you shouldn't need to make 2 table associations (pt2 and pt3) for the same table in the join. Just Include them in the select and join on the unique ID's
The "Left" is a join that is centered around you're left table so everything hangs off of that. Since you are joining the users table before the photo table, you should be able to join on its columns.
Hope this helps. Let me know if I missed something. :)
$select = array(
pt2.turl as `p_img`,
p.title as `p_title`,
p.text as `p_text`,
p.create as `p_date`,
pt2.turl as `c_img`,
u.name as `c_name`,
c.text as `c_text`,
c.create as `c_date`
);
//Set tables to variables. Just makes it easier for me
$postsTable = "posts as p"; //This will be your left table.
$userTable = "Users as u";
$commentsTable = "comments as c";
$photosTable = "photos as pt2";
$this
->db
->select($select)
->from($postsTable)
->join($userTable, "p.uid = u.id", "left")
->join($commentsTable, "p.cid = c.id", "left")
->join($photosTable, "u.photoid = pt2.id", "left")
->get();
I solved this problem myself
It would be like this:
$select= array (
//'*',
'pt.turl p_img',
'p.title p_title',
'p.text p_text',
'p.create p_date',
'pt2.turl c_img',
'c.text c_text',
'u.name c_name',
'c.create c_date'
);
$from = array (
'posts p'
);
$qry = $this
->db
->select($select)
->from($from)
->join('comments c', 'c.pid=p.id')
->join('photos pt', 'pt.id=p.pid')
->join('users u', 'u.id=c.uid')
->join('photos pt2', 'u.photoid=pt2.id')
->order_by('c.create', 'DESC')
->limit('7')
->get();
return $qry->result();

MySQL query to Laravel Eloquent

I have the following MySQL query:
SELECT
*
FROM
news_posts
WHERE
user_id = ?
AND id NOT IN (SELECT
post_id
FROM
user_read
WHERE
user_id = ?)
I want to make it more "eloquent". I've tried the following, but its (obviously) not working:
NewsPost::where('user_id', Auth::id())
->whereNotIn(function ($query) {
$query->DB::table('user_read')
->select('post_id')
->where('user_id', Auth::id());
})->get();
I'm assuming the 'where' method is executing a simple SQL query on 'new_posts' table. Can you try to rewrite this query and let mysql do the filtering for new posts? (search for LEFT OUTER JOIN on mysql)
SELECT *
FROM news_posts a
JOIN user_read b
ON a.id = b.post_id
WHERE a.user_id = ?
AND b.post_id IS NULL;
Thanks for all the replies! Got it working with the following tweaks:
NewsPost::where('user_id', Auth::id())
->whereNotIn('id', function ($query) {
$query->select('post_id')
->from('user_read')
->where('user_id', Auth::id());
})
->get();

Convert mysql query into Eloquent laravel query

I am trying to make the following query in laravel:
SELECT a.name AS subname, a.area_id, b.name, u. id, u.lawyer_id,u.active_search,
FROM subarea a
LEFT JOIN user_subarea u ON u.subarea_id = a.id
AND u.user_id = ?
LEFT JOIN branch b ON a.area_id = b.id
The idea is to obtain the subareas and see if the search is activated by the user.
The user_subarea table might have a record that matches the id of the subarea table where the active_search is equal to 0 or 1. If it doesn't exist I would like the query to return null.
While I was able to achieve this in raw SQL when I try the same with eloquent in Laravel I am not returning any value. I have done the following:
$query = DB::table('subarea')
->join('user_subarea', function($join)
{
$value = \Auth::user()->id;
$join->on( 'subarea.id', '=', 'user_subarea.subarea_id')->where('user_subarea.user_id', '=',$value);
})
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('branch.name', 'subarea.name as subarea', 'user_subarea.active_search_lawyer', 'user_subarea.id' )
->get();
Any help will be much appreciated.
I found by myself the answer it was just to add a lefjoin in the first join. It is not in the laravel docs but works too.
$query = DB::table('subarea')
->lefjoin('user_subarea', function($join)
{
$value = \Auth::user()->id;
$join->on( 'subarea.id', '=', 'user_subarea.subarea_id')->where('user_subarea.user_id', '=',$value);
})
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('branch.name', 'subarea.name as subarea', 'user_subarea.active_search_lawyer', 'user_subarea.id' )
->get();
Try this one, If you get a problem, please comment.
$value = \Auth::user()->id;
$query = DB::table('subarea')
->where('user_subarea.user_id', '=',$value)
->leftJoin('user_subarea', 'subarea.id', '=', 'user_subarea.subarea_id')
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('subarea.name AS subname','subarea.area_id', 'branch.name', 'user_subarea.id','user_subarea.lawyer_id','user_subarea.active_search')
->get();

Getting all the users who are not in the particular group in laravel 5.2

I've searched and tried a lot, but unfortunately, I didn't solve my problem. So, I'm posting my question here, please look and suggest me some solution.
I have few tables to manage users like users, profiles, groups, and group_user. Now, I want to retrieve all user name which are not the member of a particular group.
Efforts
$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('group_user AS gu', 'gu.user_id', '!=', 'users.id')
->join('groups', 'groups.id', '=', 'gu.group_id')
->where('groups.label', '=', $grouplabel)
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
I'm executing the query above to get list of users which are not the member of a particular group, but I'm not able to solve it 'til now.
If I change != to = then I get the result of all users who are in the particular group.
Table record and structure in the image.
As, you can see I have 5 users, out of which 3 users having admin group and 2 are not. If I run the query for admin group, then there should be remaining 2 user or if I run query for test group then I should get 5 users.
you don't need to change the join part, all you need to change or add up in your where() section
$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('group_user AS gu', 'gu.user_id', '=', 'users.id') // mark it as '='
->join('groups', 'groups.id', '=', 'gu.group_id')
->where('groups.label', '<>', $grouplabel) // change here, see below description
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
this will ignore the particular group (eg: profile) and will return all other users except this group (profile)
Edited
I just removed the profile section from query, to verify the relationships, once you get the results then we will add up profile section, just run this query
select * from users as u
inner join group_user as gu
on
gu.user_id = u.id
inner join groups as g
on
g.id = gu.group_id where
groups.label <> 'Tester'
Edited 2
$users = DB::table('users')
->join('profiles', 'users.id', '=', 'profiles.user_id')
->join('group_user AS gu', 'gu.user_id', '=', 'users.id')
->join('groups', 'groups.id', '=', 'gu.group_id');
//if users exists in specific group
if($users->where('groups.label', '=', $grouplabel)->count() > 0 ){
$result = $users->where('groups.label', '=', $grouplabel)
->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
} // return all users
else{
$result = $users->lists(DB::raw("CONCAT_WS(' ',profiles.firstname, profiles.lastname) AS name"),'users.id as id');
}

Categories