Hello i have this function
public function readData(){
$TableB1 = \DB::table('users')
->join('group_user', 'users.id', '=', 'group_user.user_id')
->join('groups', 'groups.id', '=', 'group_user.group_id')
->join('meetings', 'users.id', '=', 'meetings.owned_by_id')
->select(
'users.name as name',
'group_user.user_id as id',
'groups.name as groupname',
'meetings.owned_by_id as meetings'
)
->get();
return $TableB1;
}
it returns data like this :
[{"name":"Mohamed Adel","id":6,"groupname":"Team Mohamed","meetings":6},{"name":"Mohamed Adel","id":6,"groupname":"Team Mohamed","meetings":6},{"name":"Norman Osborn","id":2,"groupname":"Team Mohamed","meetings":2},{"name":"Harry Osborn","id":3,"groupname":"Team Harry","meetings":3}]
as you can see i have 2 Mohamed Adel objects how to hide them
or how to query right so i don't get repetition each time
i edited the function
public function readData(){
$TableB1 = \DB::table('users')
->join('group_user', 'users.id', '=', 'group_user.user_id')
->join('groups', 'groups.id', '=', 'group_user.group_id')
->join('meetings', 'users.id', '=', 'meetings.owned_by_id')
->select(
'users.name as name',
'group_user.user_id as id',
'groups.name as groupname',
'meetings.owned_by_id as meetings'
)
->groupBy('users.id')
->get();
i get this error : https://ibb.co/FqPd1Wz
You can simply use groupBy with query builder
public function readData()
{
$TableB1 = \DB::table('users')
->join('group_user', 'users.id', '=', 'group_user.user_id')
->join('groups', 'groups.id', '=', 'group_user.group_id')
->join('meetings', 'users.id', '=', 'meetings.owned_by_id')
->select(
'users.name as name',
'group_user.user_id as id',
'groups.name as groupname',
'meetings.owned_by_id as meetings'
)
->groupBy('users.name')
->get();
return $TableB1;
}
You can read more about groupBy here
You need to use groupBy method like this :
->groupBy('group_user.user_id')
See this documentation link Laravel Query Builder Group By
Related
I have a query where I have joined several tables together and as they have fields with identical names I have used the select query to only pick out the fields that I require. However, i also need to include the results obtained from the with query but I do not know how to specify it in the select query.
$display_tickets = ManualTicket::select('u.name as name', 'i.name as initiator', 'manual_tickets.status as status', 'manual_tickets.description as description', 'manual_tickets.location as location', 'manual_tickets.created_at as created_at', 'manual_tickets.initiator_id as initiator_id', 'manual_tickets.id as manual_ticket_id','manual_tickets.manual_ticket_log as manual_ticket_log_id')
->leftJoin('users as u', 'u.id', '=', 'manual_tickets.user_id')
->leftJoin('users as i', 'i.id', '=', 'manual_tickets.initiator_id')
->where(function ($checkClients) use($target_client_id){
$checkClients->where('u.client_id', '=', $target_client_id)
->orWhere('i.client_id', '=', $target_client_id);
})
->whereBetween('manual_tickets.created_at', [$start_date->toDateString(), $end_date->addDays(1)->toDateString()])
->with('manual_ticket_log')
->orderBy("created_at", "DESC")->get();
I tried to include the manual ticket logs through the use of the code below but it says that there is no such field in the manual ticket table.
'manual_tickets.manual_ticket_log as manual_ticket_log_id')
In short, how can i include the results of the with relationship from ->with('manual_ticket_log') into the select statement
--EDIT-- (Tried to replace join with with queries)
However there appears to be any error with my SQL query which i believe stems from the whereHas portion of the code which results in this error being shown.
strtolower() expects parameter 1 to be string, object given
$display_tickets = ManualTicket::select('*')
->with('user')
->with('initiator')
->with('manual_ticket_log')
->where(function ($checkClients) use($target_client_id){
$checkClients->whereHas('user', function ($checkClient) use($target_client_id){
$checkClient->where('client_id', '=', $target_client_id);
})
->orWhere($checkClients->whereHas('initiator', function ($checkClient2) use($target_client_id){
$checkClient2->where('client_id', '=', $target_client_id);
}));
})
->whereBetween('manual_tickets.created_at', [$start_date->toDateString(), $end_date->addDays(1)->toDateString()])
->orderBy("created_at", "DESC")->get();
you should join that table to get fields form:
$display_tickets = ManualTicket::select('u.name as name', 'i.name as initiator', 'manual_tickets.status as status', 'manual_tickets.description as description', 'manual_tickets.location as location', 'manual_tickets.created_at as created_at', 'manual_tickets.initiator_id as initiator_id', 'manual_tickets.id as manual_ticket_id','manual_tickets.manual_ticket_log as manual_ticket_log_id',
'manual_tickets.manual_ticket_log as manual_ticket_log_id')
->leftJoin('users as u', 'u.id', '=', 'manual_tickets.user_id')
->leftJoin('users as i', 'i.id', '=', 'manual_tickets.initiator_id')
->leftJoin('manual_ticket_logs',function ($join){
$join->on('manual_ticket_logs.manual_ticket_id', '=', 'manual_tickets.id')
->on('manual_ticket_logs.id', '=', \DB::raw("(select max(id) from manual_ticket_logs WHERE manual_ticket_logs.manual_ticket_id = manual_tickets.id)"));
})
->where(function ($checkClients) use($target_client_id){
$checkClients->where('u.client_id', '=', $target_client_id)
->orWhere('i.client_id', '=', $target_client_id);
})
->whereBetween('manual_tickets.created_at', [$start_date->toDateString(), $end_date->addDays(1)->toDateString()])
->with('manual_ticket_log')
->orderBy("created_at", "DESC")->get();
please make sure of the fields names in the newest join clause.
You can take advantage of Laravel relationships to make the code more readable and avoid an expensive leftJoin:
$display_tickets = ManualTicket::select('*')
->with([ 'user', 'initiator', 'manual_ticket_log' ])
->where(function ($checkClients) use($target_client_id){
$checkClients->whereHas('user', function ($checkClient) use($target_client_id){
$checkClient->where('client_id', '=', $target_client_id);
})->orWhereHas('initiator', function ($checkClient2) use($target_client_id){
$checkClient2->where('client_id', '=', $target_client_id);
}));
})->whereBetween('created_at', [$start_date->toDateString(), $end_date->addDays(1)->toDateString()])
->orderBy("created_at", "DESC")
->get();
You can try this
$display_tickets = ManualTicket::select('u.name as name', 'i.name as initiator', 'manual_tickets.status as status', 'manual_tickets.description as description', 'manual_tickets.location as location', 'manual_tickets.created_at as created_at', 'manual_tickets.initiator_id as initiator_id', 'manual_tickets.id as manual_ticket_id','manual_tickets.manual_ticket_log as manual_ticket_log_id')
->leftJoin('users as u', 'u.id', '=', 'manual_tickets.user_id')
->leftJoin('users as i', 'i.id', '=', 'manual_tickets.initiator_id')
->where(function ($checkClients) use($target_client_id){
$checkClients->where('u.client_id', '=', $target_client_id)
->orWhere('i.client_id', '=', $target_client_id);
})
->whereBetween('manual_tickets.created_at', [$start_date->toDateString(), $end_date->addDays(1)->toDateString()])
->with(['manual_ticket_log' => function($query) {
$query->select('field1', 'field2');
}])
->orderBy("created_at", "DESC")->get();
Or do this like
->with('manual_ticket_log:field1, field2')
I want to get data from table role_users , column role_id . For the moment, I have this in my controller :
$data['contact_users'] = DB::table('contacts')
->join('users' , 'users.id', '=', 'contacts.contact_id')
->join('industries' , 'industries.id', '=', 'users.industry_id')
->join('countries' , 'countries.id', '=', 'users.country_id')
->join('organization_types' , 'organization_types.id', '=', 'users.organization_type_id')
->select('users.*','industries.industry','countries.country','organization_types.organization_type')
->where('contacts.contact_id','!=',$id)
->where('users.deleted_at','=',NULL)
->whereIn('contacts.user_id', $contact_id)
->whereNotIn('contacts.contact_id', $contact_id)
->whereNotIn('contacts.contact_id', $inviter_id)
->groupBy('contact_id')
->take(4)
->get();
I'm using it in view with this code :
{{$contact->industry_id}} or {{$contact->country_id}}
I need to use something like this.
{{$contact->role_id}}
which is working for every user.But I need to get data from role_users,column role_id.I don't know how to use ->join() and I need it so much.Thank you.
I added one extra join and one select field at the end,
$data['contact_users'] = DB::table('contacts')
->join('users', 'users.id', '=', 'contacts.contact_id')
->join('industries', 'industries.id', '=', 'users.industry_id')
->join('countries', 'countries.id', '=', 'users.country_id')
->join('organization_types', 'organization_types.id', '=', 'users.organization_type_id')
->join("role_users", "role_users.user_id","=","users.id")
->select('users.*', 'industries.industry', 'countries.country', 'organization_types.organization_type', "role_users.role_id")
->where('contacts.contact_id', '!=', $id)
->where('users.deleted_at', '=', null)
->whereIn('contacts.user_id', $contact_id)
->whereNotIn('contacts.contact_id', $contact_id)
->whereNotIn('contacts.contact_id', $inviter_id)
->groupBy('contact_id')
->take(4)
->get();
I'm trying to join the below 3 tables. The issue is in the part where I want to join posts.id to the shares.post_id using 'AND' operator which is not correct. What is the correct syntax to do that ?
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', 'posts.user_id', '=', 'users.id' , 'AND' , 'posts.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
Try aliasing:
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts as p1', 'p1.user_id', '=', 'users.id' , 'AND' , 'p1.id', '=', 'shares.post_id')
->join('posts as p2', 'p2.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
You cannot join a table twice with no alias.
Although if you have 2 clauses in your join, I think you ought to do as in the documentation :
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts as p1', , function($join){
$join->on('p1.user_id', '=', 'users.id')
->on('p1.id', '=', 'shares.post_id')
})
->join('posts as p2', 'p2.id', '=', 'shares.post_id')
->where('shares.user_id', '=', $myFriends)
->get();
Edit (with one posts join):
$sharedPosts = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('posts', , function($join){
$join->on('posts.user_id', '=', 'users.id')
->on('posts.id', '=', 'shares.post_id')
})
->where('shares.user_id', '=', $myFriends)
->get();
I'm new to Laravel and I'm still working on my Project using this Framework. In my project, I use an SQL JOIN but I don't know how to retrieve/fetch data on the joined table.
Can anybody help me on this one?
I really appreciate your help. Thanks!
Here is a simple example!
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
$users = \DB::table('users')
->join('contacts', 'contacts.user_id', '=', 'users.id')
->join('orders', 'orders.user_id', '=', 'users.id')
->select('users.id as id', 'contacts.phone as phone', 'orders.price as price')
//Where condition here if exist
->get();
$users will now have the array of values.Loop it in an array if necessary.
Eg:
forearch($users as $user)
{ $user->id;
$user->phone;
}
Like that.
TABLES X(ID,A_ID)A(ID,B_ID),B(ID,C_ID),C(ID,D_ID)D(id,VALUE)
I want to retrieve the value of D table using laravel syntax on basis of X table Id and perform a JOIN with other tables.
Please post answers only in laravel syntax. In other format I can do. I am new to it so.
X::select('value')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
But it is not working. please provide me proper solution. Right now I am using PHP logic to get the value, rather than optimise the query.
There are many ways, but this is the basic:
$rows = DB::table('shares')
->join('users', 'users.id', '=', 'shares.user_id')
->join('follows', 'follows.user_id', '=', 'users.id')
->where('follows.follower_id', '=', 3)
->get();
doc
Try this:
DB::table('X')
->join('a', 'X.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->select('D.value')
->where('x.id', '=', $val)
->get();
$result = X::select('a.value', 'd.*')
->join('a', 'x.a_id', '=', 'a.id')
->join('b', 'a.b_id', '=', 'b.id')
->join('c', 'b.c_id', '=', 'c.id')
->join('d', 'c.d_id', '=', 'd.id')
->where('x.id', '=', $val)
->get();
foreach ($result as $row) {
# code...
}