Answer the question
In order to leave comments, you need to log in
How to sort by adjacent Laravel table?
Have a request
Card::leftJoin('comments', 'cards.id', '=', 'comments.card_id')
->select('cards.id')
->orderBy('comments.created_at', 'desc')
->groupBy('id')
->get();
Answer the question
In order to leave comments, you need to log in
It turned out to be done through the filter as follows:
Card::leftJoin('comments', 'cards.id', '=', 'comments.card_id')
->where('comments.type', 'comment')
->select('cards.*')
->orderBy('comments.created_at', 'desc')
->groupBy('comments.id')
->get()
->filter(function($item, $key) {
if(!in_array($item->id, $this->alreadySelectedCards)) {
$this->alreadySelectedCards[] = $item->id;
return true;
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question