@manomahe Here you are using groupBy and OrderBy combine which not work proper in your case, try to do it with sub query to achieve result properly.
$sub = DB::table('reminders_calendar')
->select('invoice_id', DB::raw('MAX(id) as max_id'))
->groupBy('invoice_id');
$def_reminders = DB::table('invoices AS inv')
->joinSub($sub, 'max_remcal', function ($join) {
$join->on('inv.id', '=', 'max_remcal.invoice_id');
})
->join('reminders_calendar AS remcal', function ($join) {
$join->on('inv.id', '=', 'remcal.invoice_id')
->on('remcal.id', '=', 'max_remcal.max_id');
})
->where([
'inv.user_id' => 1,
'inv.is_deleted' => '0',
])
->where('inv.reminder_status', '!=', 'Enabled')
->whereDate('remcal.date_when', '2024-01-03')
->whereIn('remcal.reminder_type', ['InProgress', 'Waiting'])
->select('remcal.*', 'inv.*')
->orderBy('remcal.id', 'desc')
->get();
Try this version it should be worked. Let me know your thoughts on this.