I am getting data from join in laravel but i cant get rides id using get or pluck method in query.
$datas = \App\Models\Order::find($id);
$new = explode(',',$id);
$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->get();
here is the data
enter image description here
i want to get data of rides.id from query
whenever i use get or pluck method i got error
`
$datas = \App\Models\Order::find($id);
$new = explode(',',$id);
$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->get('id');
`
enter image description here
In the second image you posted error show that 'id' field is ambiguous which means there are multiple columns named 'id' and it is true.
There are three 'id' columns coming from orders table, jobs table and rides table.
So you need to tell that which 'id' column you want to extract.
For this purpose you can try following code.
$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->get('rides.id')
OR
$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->select('rides.id')
->get()
these both should work and return you the collection of rides ids.
Related
I have 4 tables clinics , locations , services & location_services in which relations are clinicid is present in locations and both serviceid and location id is present in location_services table.
My requirement is that i want to retrive all the cinics and their coreesponding locations and services .
But when i tried it is retrving the results of only one clinic id , i dont know how can i retrive complete list
following is my code
$clinic = Clinic::find($id);
$locations = Location::where('clinicID', $id)->get();
$locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid','locations.locationName as locname')
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->toSql();
dd($locationservices);
die();
Please help me to solve this
$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();
The following is my query which may output more than one row. I need to store the user_id field in each row in the $send_user_id variable below, so that I will use a foreach loop in the blade. I'm currently receiving (Trying to get property of non-object) error.
$j_request = DB::table('grpusrs')
->join('users', 'users.id', '=', 'grpusrs.user_id')
->join('groups', 'grpusrs.group_id', '=', 'groups.id')
->where('groups.owner_id', $user_id)
->where('grpusrs.join_request', 1)
->get();
$send_user_id = $j_request->user_id;
Also this query joins two tables, so how do I specify the table that has the required field? (i.e. I want the user_id that is in the 'Grpusrs' table not in the 'Groups' table)
If you need to retrieve multiple user_id you can loop through $j_request object and collect every user_id inside an array.
$j_request = DB::table('grpusrs')
->join('users', 'users.id', '=', 'grpusrs.user_id')
->join('groups', 'grpusrs.group_id', '=', 'groups.id')
->where('groups.owner_id', $user_id)
->where('grpusrs.join_request', 1)
->select('users.*', 'contacts.phone', 'orders.price')
->get();
$send_user_id = [];
$j_request->each(function($item) use ($send_use_id){
$send_user_id[] = $item->user_id;
});
If you only need one user_id you should be more "precisely" in your query (get just one record). After that, you can get user_id simply with $j_request->user_id
im trying to fetch multiple Id's from one array for then using it on a query relationship.
This is what I tried:
$following = $user->following;
$followingId = $following->id;
I get this error:
'Undefined property: Illuminate\Database\Eloquent\Collection::$id'
If I log $following, I get the following array, as you can see I'm following 4 different users and I need to get those id's:
local.INFO: [{"id":32,"email":"test#mail.com","username":"test#mail.com","photo":"97dfebf4098c0f5c16bca61e2b76c373","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":1,"pivot":{"follower_id":1,"followed_id":32}},{"id":69,"email":"fhamada#paginar.net","username":"fhamada#paginar.net","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":69}},{"id":79,"email":"sandrastun#gmail.com","username":"sandrastun#gmail.com","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":79}},{"id":101,"email":"cristobalalfonzo#outlook.com","username":"cristobalalfonzo#outlook.com","photo":"placeholder-square.jpg","cover_photo":"default-no-image.jpg","last_access":null,"activation_code":null,"is_sponsor":0,"is_admin":0,"displayname":null,"followers_amount":1,"winwins_amount":0,"pivot":{"follower_id":1,"followed_id":101}}]
Query relationship:
$activities = DB::table('notifications')
->join('users', 'users.id', '=', 'notifications.sender_id')
->join('followers', 'followed_id', '=', 'users.id')
->join('user_details', 'user_details.id', '=', 'notifications.user_id')
->join('winwins', 'winwins.id', '=', 'object_id')
->leftJoin('groups', 'notifications.object_id', '=', 'groups.id')
->where('notifications.sender_id', '=', $user->id)
->orWhere('winwins.id', '=', 'notifications.object_id')
->orWhere('sender_id', '=', $user->id)
->orWhere('followers.followed_id', '=', $followingId)
->orderBy('sent_at', 'desc')
->skip($page * $amount)
->take($amount)
To fetch multiple columns from a collection (e.g. multiple IDs from your following collection), use this:
$followingId = $following->pluck('id')->toArray();
I'm struggling to get unique results when using the DISTINCT method in laravel. This is my query
//Get users that are part of this sector
$users = DB::table('users')->distinct()
->join('projects', 'users.id', '=', 'projects.userID')
->where('projects.sectorID', '=', $sector->sectorID)
->get();
dd($users);
The result shows 3 users with the same ID, when I only want one of them in the results.
How should I change my query?
Try to use group by id
$users = DB::table('users')
->join('projects', 'users.id', '=', 'projects.userID')
->where('projects.sectorID', '=', $sector->sectorID)
->groupBy('users.id')
->get();
dd($users);
Any way of defining an AS for a query??
I have tried the following:
$data = News::order_by('news.id', 'desc')
->join('categories', 'news.category_id', '=', 'categories.id')
->left_join('users', 'news.user_id', '=', 'users.id') // ['created_by']
->left_join('users', 'news.modified_by', '=', 'users.id') // ['modified_by']
->paginate(30, array('news.title', 'categories.name as categories', 'users.name as username'));
The problem is that ['name'] from categories will be replaces with the one from users. Any way of having them with different names?
Having the aliases above... how can I create an alias where both joins return users.name ?
paginate() method's second parameter accepts array of table columns to select in the query. So this part:
paginate(30, array('news.title, category.name'));
must be like this:
paginate(30, array('news.title', 'category.name'));
UPDATE (after you changed the question)
Try this:
->paginate(30, array('news.title', 'categories.name as category_name', 'users.name as user_name'));
UPDATE 2 (after you changed the question, again)
You can use alias on tables, too:
$data = News::order_by('news.id', 'desc')
->join('categories', 'news.category_id', '=', 'categories.id')
->join('users as u1', 'news.user_id', '=', 'u1.id') // ['created_by']
->join('users as u2', 'news.modified_by', '=', 'u2.id') // ['modified_by']
->paginate(30, array('news.title', 'categories.name as categories', 'u1.name as creater_username', 'u2.name as modifier_username'));
More simple and plain answer to this question is and what I was looking for that Eloquent supports aliases directly with table names or columns, for example:
$users = DB::table('really_long_table_name AS t')
->select('t.id AS uid')
->get();