Sorting
Column sorting
To make a column sortable, you may pass the sortable
argument to the column's 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';
}
If you want to sort in descending order by default, simply prefix the column name with a minus sign:
protected ?string $defaultSort = '-name';
Sort by Relationships
If you want to sort by a column on a related Model, you must install the Eloquent Power Joins package by Kirschbaum:
composer require kirschbaum-development/eloquent-power-joins
Customizing the Query
You can customize how the sorting is applied to the query by passing a closure to the sortUsing()
method. It takes two arguments: 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);