Sorting
Making Columns Sortable
php
Column::make('name', 'Name')->sortable()
TextColumn::make('email', 'Email')->sortable()Non-sortable columns show no sort indicator. Sortable columns show ⇅ when inactive, ▲ when ascending, ▼ when descending.
Default Sort
php
class ServerTable extends Table
{
protected string $defaultSort = 'created_at';
protected string $defaultSortDir = 'desc';
}Sort Priority
Sorting uses a three-tier priority:
- User sort (highest) -
?sort_by=name&sort_dir=ascin the URL - Pre-existing query orders -
orderByclauses already on the query - Default sort (lowest) -
$defaultSort/$defaultSortDir
php
// Pre-existing order preserved until user explicitly sorts
ServerTable::make(
Server::query()->orderByRaw('FIELD(status, "active", "pending", "failed")')
);Sort Behaviour
- Clicking a sortable header: first click → ascending, second click → descending, third click → clears sort (returns to default)
- Invalid
sort_byvalues fall back to the default sort - Invalid
sort_dirvalues default todesc
Custom Sort Key
Use accessor() when the sort column differs from the display column:
php
Column::make('author', 'Author')
->accessor('users.name') // sorts by users.name
->sortable()
->value(fn ($m) => $m->user->name)