Skip to content

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:

  1. User sort (highest) - ?sort_by=name&sort_dir=asc in the URL
  2. Pre-existing query orders - orderBy clauses already on the query
  3. 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_by values fall back to the default sort
  • Invalid sort_dir values default to desc

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)