How to default sort by column that does not exist in the table? #6225
Replies: 2 comments
-
We can't sort by a column that does not exist in the database because it would be very inefficient across large datasets. Maybe you need a generated column in the database |
Beta Was this translation helpful? Give feedback.
-
I know it's been a long time ago, but maybe someone might need the solution. return $table
->modifyQueryUsing(function ($query) {
$subQuery = DB::table('registration_activities')
->select('registration_id', DB::raw('MAX(created_at) as newest'))
->groupBy('registration_id');
return $query->joinSub($subQuery, 'latest_status_updates', function ($join) {
$join->on('registrations.id', '=', 'latest_status_updates.registration_id');
})
->orderByRaw("
CASE
WHEN registrations.status IN ('REGISTERED', 'CANCEL', 'PENDING') THEN 1
ELSE 0
END
");
})
->defaultSort('latest_status_updates.newest', 'asc') So, this is inside RegistrationResource which has Registration model. I need Registration list to be sorted by default based on it's latest registration_activities. Then in a table column, i displayed the date differences from now and make it sortable as well : TextColumn::make('followup_since')
->label('Last FU')
->sortable(query: function(Builder $query, string $direction): Builder {
return $query->orderBy('latest_status_updates.newest', $direction);
})
->getStateUsing(function ($record) {
$latestStatusUpdate = $record->status_updates()->latest()->first();
if ($latestStatusUpdate) {
return $latestStatusUpdate->created_at->diffForHumans(now(), CarbonInterface::DIFF_ABSOLUTE, true);
}
}),
|
Beta Was this translation helpful? Give feedback.
-
I am generating a custom column in my table that sums the amount column. So, technically the sum column does not exist in the database and a temporary column. But I want to default sort by the sum column.
Calling defaultSort works.
but when i use any bulk actions, I get the error that column fines_sum_amount does not exist in the database.
and here is my bulk action
error
Beta Was this translation helpful? Give feedback.
All reactions