I think you are using both get and all in your query and it won't work, try to change it and use get() or all() in your query with that use get()->toArray() it also help to achieve what you need.
Sep 20, 2019
3
Level 1
Query not ordering by start_date?
Hi there, I'm new to the Laracasts forum and I look forward to using this forum to learn more about Laravel.
I've created a query below to output events for a user. It all works fine, except to the orderBy doesn't actually order the events by start_date. Can anyone assist with this query?
I've used the raw SQL for this query and it seems to work, so I'm not sure why it won't order correctly. I've even turned off the JSON formatter chrome plugin as that has bitten me before in the past!
$events = EventUser::with(['event' => function($query) use($compulsory) {
$query->where('deleted', 0)->where('start_date', '<', NOW())->where('compulsory_id', $compulsory->id)->orderBy('start_date', 'DESC');
}])
->where('user_id', $user_id)
->get()
->pluck('event')
->flatten()
->values()
->all();
Many thanks
Level 13
Please or to participate in this conversation.