dimanche 2 juin 2019

Why Laravel joined query modifies order if distinct is used?

I have the following query:

$recentTasks = Task::where('assigned_to','=',$user->id)->join('task_comments','task_comments.task_id','=','tasks.id')->orderBy('task_comments.updated_at','DESC')->select('tasks.*')->limit(10)->get();

What I need is a list of tasks that had recent comments. So I joined the comments table and ordered by it. It works and recently commented tasks are at the top. But it has duplicates, so one task can appear multiple times. If I insert a ->distinct() or ->distinct('tasks.id') then the duplicates are gone, but the sort by order is wrong.

How can I keep the sorting order but remove duplicates?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire