Sorting
Inertia Table supports column-level sorting out of the box. Mark any column as sortable, optionally configure a default sort order on the Table, and Inertia Table generates the click-to-sort UI and runs the matching ORDER BY clauses against your Eloquent query, including across related models, without writing the joins yourself.
Column Sorting
To make a column sortable, pass sortable: true to its make() method:
TextColumn::make('name', sortable: true);Alternatively, you may use the sortable() and notSortable() methods:
TextColumn::make('name')->sortable();
TextColumn::make('name')->notSortable();Default Table Sorting
You may set the $defaultSort property on your Table class to define the default sorting column:
class Users extends Table
{
protected ?string $defaultSort = 'name';
}To sort in descending order by default, prefix the column name with a minus sign:
protected ?string $defaultSort = '-name';Sort by a Mapped Value
Status columns are often mapped to a human-readable or translated string via the mapAs method. Usually you want to sort by this mapped value rather than the original. Call sortUsingMap() on the column to do so. This requires mapAs to be called first with an array:
TextColumn::make('status', sortable: true)->mapAs([
'is_active' => 'Actief',
'is_deleted' => 'Verwijderd',
'is_pending' => 'In afwachting',
])->sortUsingMap();To sort by a different mapping, pass the mapping array to sortUsingMap():
TextColumn::make('status', sortable: true)->sortUsingMap([
'is_active' => 1,
'is_deleted' => 3,
'is_pending' => 2,
]);Under the hood, this calls sortUsingPriority(), which sorts the column based on the priority of the given values. The example above sorts in the order is_active, is_pending, is_deleted. The same result may be achieved directly with sortUsingPriority():
TextColumn::make('status', sortable: true)->sortUsingPriority([
'is_active', 'is_pending', 'is_deleted',
]);Sort by Relationships
To sort by a column on a related model, install the Eloquent Power Joins package by Kirschbaum:
composer require kirschbaum-development/eloquent-power-joinsCustomizing the Query
You may customize how sorting is applied to the query by passing a closure to the sortUsing() method. It receives the Eloquent Query Builder and the SortDirection enum:
use Illuminate\Database\Eloquent\Builder;
use InertiaUI\Table\SortDirection;
TextColumn::make('name')->sortUsing(function (Builder $query, SortDirection $direction) {
$query->orderBy('name', $direction->value);
});Alternatively, you may use the sortUsing argument on the column's make() method. Besides a Closure, you may also pass an invocable class:
TextColumn::make('name', sortUsing: new CustomSortByName);