Tricks

Sorting Table with spatie/eloquent-sortable

Sep 16, 2022
Lloric Mayuga Garcia
Table builder, Integration

When using spatie/eloquent-sortable, you have a extensive sorting function to your eloquent.

In your resource component add this to your table():

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('order_column', '#')
->toggleable()
->sortable(),
// ...
])
// ...
->reorderable('order_column')
->defaultSort('order_column');
}

Add this to your ListRecords page:

public function reorderTable(array $order): void
{
static::getResource()::getModel()::setNewOrder($order);
}

And that's it!, you have have a working sortable eloquent by Spatie.

No comments yet…