Reputation: 1
I just need to order my records according to updated, and then group them according to card_id.
This is my code:
$triages = Triyage::latest('updated_at')->groupBy('card_id')->paginate(8);
return view('Admin.Opd.card_opd', compact('triages'));
Upvotes: 0
Views: 1326
Reputation: 25906
Use a subquery JOIN:
$join = Triyage::select('card_id', DB::raw('max(updated_at) updated_at'))
->groupBy('card_id');
$sql = '(' . $join->toSql() . ') as latest';
$triages = Triyage::join(DB::raw($sql), function($join) {
$join->on('triyages.card_id', 'latest.card_id')
->on('triyages.updated_at', 'latest.updated_at');
})->paginate(8);
Upvotes: 1