This trick was submitted before the admin panel had reordering functionality. You can find out more about that in the docs.
Install the spatie/eloquent-sortable package .
Create table actions to move the record up and down:
Tables\Actions\Action::make('up') ->action(fn (YourModel $record) => $record->moveOrderUp()),Tables\Actions\Action::make('down') ->action(fn (YourModel $record) => $record->moveOrderDown()),
protected function getTableQuery(): Builder{ return parent::getTableQuery()->orderBy('your_order_column');}
even nicer:
please note the secon icon doe snot show if make is set to empty string
Is there any way to use it with belong_to_many relationship?
I need to set the sort order in my relation table that is used to attach between 2 models.
Check out the documentation here: https://github.com/spatie/eloquent-sortable/tree/main#grouping
Use the
buildSortQuery()
function to create the query that pulls together all the records that the current record is a member of.The Spatie ordering trait provides a scope for ordering, so you can do this for consistency:
The default ordering is ascending, but you can reverse that: