Card::leftJoin('comments', 'cards.id', '=', 'comments.card_id')
->select('cards.id')
->orderBy('comments.created_at', 'desc')
->groupBy('id')
->get();
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;
}
});