jeudi 29 juin 2023

How to get sorted one to many field with respect to max of a column in Laravel?

I am using Laravel 5.1 and want to get items sorted with respect to one column of the many field.

I have members and subscriptions. Each member has multiple subscriptions with end date as one of the column. I want to sort members with respect to subscriptions with maximum end dates.

I am using the code below but it selects first subscriptions and sort members with respect to first subscriptions.

Member::join('trn_subscriptions as subs', 'subs.member_id', '=', 'mst_members.id')
            ->select('mst_members.*', DB::raw('max(subs.end_date) latest_sub'))
            ->groupBy('mst_members.id')
            ->orderBy('latest_sub','desc')
            ->where('subs.end_date', '<', Carbon::today());

How can I achive sorting with respect to subscriptions for each member having maximum end dates below certain date?

Thank you in advance.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/NDX5nGK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire