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, a chevron up when ascending, and a chevron down when descending.
Default Sort
Use the - prefix for descending, just like the URL parameter format:
php
class ServerTable extends Table
{
protected string $defaultSort = '-created_at'; // desc
}
class UserTable extends Table
{
protected string $defaultSort = 'name'; // asc
}Sort Priority
Sorting uses a three-tier priority:
- User sort (highest) -
?sort=name(asc) or?sort=-name(desc) in the URL - Pre-existing query orders -
orderByclauses already on the query - Default sort (lowest) -
$defaultSort
php
// Pre-existing order preserved until user explicitly sorts
ServerTable::make(
Server::query()->orderByRaw('FIELD(status, "active", "pending", "failed")')
);Sort Param Format
Sorting uses a single sort URL parameter with a - prefix for descending:
| URL | Meaning |
|---|---|
?sort=name | Sort by name ascending |
?sort=-name | Sort by name descending |
| (no sort param) | Default sort applies |
When using an identifier (e.g. 'users'), the param becomes ?usersSort=name.
Sort Behaviour
- Clicking a sortable header: first click - ascending, second click - descending, third click - clears sort (returns to default)
- Invalid sort column values fall through to pre-existing query orders, then to the default sort
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)Sorting Relationship Columns
Dot-notation columns automatically use the dot path as their sort key. For sorting to work, the relation table must be joined in the query - eager loading is not sufficient for SQL ORDER BY:
php
// In your table class or via a beforeQuery hook
Table::beforeQuery(ServerTable::class, function ($query, array &$columns) {
$query->join('users', 'servers.owner_id', '=', 'users.id')
->select('servers.*');
});
// Then the dot-notation column is sortable
Column::make('owner.name', 'Owner')->sortable()
// Sort key sent to DB: owner.name (interpreted as table.column)