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();
Related
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.
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
I need to get specific fields from the database and then paginate the results but I am not sure where to place ->paginate().
My code is as below:
Upload::where('sponsor_id', Request::segment(2))
//->paginate(20)
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->get([
'uploads.filename',
'users.first_name',
'users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at',
])
I'm getting the correct results without paginating but I would like to paginate the results.
First, you need to use select then pass in pagination.
Upload::where('sponsor_id', Request::segment(2))
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->select('uploads.filename','users.first_name','users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at')
->pagiate(10);
You can use select method for filtering columns. And then put paginate method end of your query.
Finaly your query will be like this:
Upload::where('sponsor_id', Request::segment(2))
->join('users', 'users.id', 'uploads.uploaded_by')
->orderBy('uploads.created_at', 'DESC')
->select(
'uploads.filename',
'users.first_name',
'users.surname',
'uploads.errors',
'uploads.statistics',
'uploads.created_at',
)
->paginate(20);
What I am trying to achieve is to allow teachers to import a student into different classes.
Note: A student can be multiple classes.
The problem is that when I show the list of students in a select dropdown it should return all students except for students that are not in this class (the class being the page that I am on, app.com/classes/5 for example).
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'asc')
->get();
This works and shows all students that are not in this specific class BUT if a student that's in this class and another class their name appears in the list and as duplicate names.
What can I do?
When MySQL's only_full_group_by mode is turned on, it means that strict ANSI SQL rules will apply when using GROUP BY
You should try to select fields from schema on which you can apply group by instead of select *.
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->select('users.id', 'other fields you used')
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();
Not IN is also useful in your case
User::select('fields you used')
->role('student')
->whereNotIn('id', DB::table('group_user')->where('group_id', $id)->pluck('user_id')) // $id = 5
->orderBy('name', 'ASC')
->get();
Modify your query to use distinct() like so;
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->distinct()
->get();
You could also groupBy('users.id')
$students = User::join('group_user', 'users.id', '=', 'group_user.user_id')
->role('student')
->where('group_user.group_id', '!=', $id)
->orderBy('users.name', 'ASC')
->groupBy('users.id')
->get();
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);