Use join because eager loading runs 2nd query, so you'll end up with jobs ordered by whatever you like but it won't affect users order.
jarektkaczyk said:
Use join because eager loading runs 2nd query, so you'll end up with jobs ordered by whatever you like but it won't affect users order.
Can you please write the suggested query?
In fact I didn't notice you wanted to load jobs for a signle user, my bad! So if that's the case, then you are free to use eager loading / lazy eager loading:
Auth::user()->load(['jobs' => function ($q) {
$q->orderBy('job_user.created_at','asc');
}]);
Ok I found a solution myself, It's as simple as this:
Auth::user()->jobs()
->orderBy('job_user.created_at', 'desc')
->get();
Another Solution for this:
Auth::user()->jobs()
->orderBy('pivot_created_at', 'desc')
->get();
In model, you should define created_at
from the pivot with withPivot
method
public function jobs()
{
return $this->belongsToMany('Jobs')->withPivot('created_at');
}
ammont said:
Ok I found a solution myself, It's as simple as this:
Auth::user()->jobs() ->orderBy('job_user.created_at', 'desc') ->get();
Thanks! You saved my late
$saved = Service::findOrFail($request->service_id)->products()->orderBy('product_service.updated_at', 'desc')->first();
That was my issue...
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community