Using closure with sortable on TextColumn
I've set up my database schema using Eloquent models, and I'm trying to use the customer model in my sortable() method:
TextColumn::make('customer.postcode')
->sortable(query: function (Builder $query, string $direction): Builder {
return $query->whereHas('customer', function (Builder $q) use ($direction) {
$q->orderBy('postcode', $direction);
});
})
...however the sorting doesn't actually seem to work. It sorts the table records but not in any obvious order.
2 Replies
Your query doesn't make sense. Your query reads as:
Give me all records where the model has customers and sort those customers before checking whether it has any.
If you want to sort on a related table, you need to
JOIN
Ah ok that makes sense. Is there any way to do that without needing to specify actual table names?