Nechalon
03/10/2023, 2:39 PMifox
03/10/2023, 4:38 PMprotected $indexColumns = [
'title' => [
'title' => 'Title',
'field' => 'title',
'sort' => true,
],
'eventType' => [
'title' => 'Type',
'relationship' => 'eventType',
'field' => 'title',
'sort' => true,
],
];
public function order($query, array $orders = [])
{
if (array_key_exists('eventTypeTitle', $orders)) {
$direction = $orders['eventTypeTitle'];
unset($orders['eventTypeTitle']);
$query->orderByEventType($direction);
}
return parent::order($query, $orders);
}ifox
03/10/2023, 4:39 PMpublic function scopeOrderByEventType($query, $direction = 'asc')
{
return $query
->select('events.*')
->leftJoin(
'event_types',
'events.event_type_id',
'=',
'event_types.id'
)
->leftJoin(
'event_type_translations',
'event_types.id',
'=',
'event_type_translations.event_type_id'
)
->where('event_type_translations.locale', locale())
->orderBy('event_type_translations.title', $direction);
}
You won't need that complexity because your fields are not translatable but hopefully that gives you some inspirationifox
03/10/2023, 4:40 PMifox
03/10/2023, 4:45 PMpublic function order($query, array $orders = [])
{
if (array_key_exists('userName', $orders)) {
$direction = $orders['userName'];
unset($orders['userName']);
$query->with('user')->orderBy('user.name');
}
return parent::order($query, $orders);
}