- How do I install akaunting/laravel-sortable in a Laravel application?
- Install it with Composer using composer require akaunting/laravel-sortable, then add the package trait to any Eloquent model that needs sortable behavior. You also need a position column on that model's database table, which you add yourself through a normal Laravel migration. Laravel package auto-discovery handles the service provider, so no manual registration is usually required.
- Which Laravel and PHP versions does laravel-sortable 3.0 support?
- Version 3.0 targets modern PHP and Laravel, aligning with Laravel 13 support and requiring PHP 8.2 or newer. Support for Laravel 9 and PHP 8.0 and 8.1 was dropped in this major release. Confirm the exact tested matrix in the package's composer.json and CI configuration before upgrading, since the supported Laravel 10, 11, and 12 ranges should be verified rather than assumed.
- How do I make an Eloquent model sortable with a position column?
- Add an integer position column to the model's table in a migration, then use the package's sortable trait on the model class. The trait maintains that position value as records are moved, so you do not need to write update logic by hand. Existing rows should be given sequential position values in a migration or seeder before you start reordering.
- How do I move a model up or down in the sort order?
- The package exposes helper methods for moving a record up or down, so you can call something like moveUp() or moveDown() on an instance rather than recalculating positions manually. The trait then updates the affected position values so the order stays consistent. Always re-query or refresh the model after moving it if you rely on the position attribute in the same request.
- Can I move a record before or after a specific record?
- Yes. Beyond simple up and down moves, the package provides helpers for placing a record before or after another model instance, which is useful for drag-and-drop interfaces where the front end sends a target record. You pass the target model to the helper and the trait reshuffles the positions accordingly. This keeps position logic out of your controllers when users drop an item in an arbitrary spot.
- Does laravel-sortable support sorting within groups, like per category or parent record?
- The package maintains a single position column, so scoped or grouped ordering is not guaranteed out of the box in the overview documentation. If you need per-category or per-parent ordering, verify the behavior against the README or add a composite constraint yourself and cover it with tests. Many teams implement grouped sorting by scoping the query and adding a second column such as category_id to the ordering logic.
- Is upgrading from laravel-sortable 2.x to 3.0 a breaking change?
- Yes, 3.0 is a major release that raises the platform floor to PHP 8.2 and modern Laravel, so it is a breaking change for legacy stacks. Applications still on Laravel 9 or PHP 8.0 and 8.1 should stay pinned to 2.x until the platform is upgraded. Review the upgrade guide and run your test suite in CI before adopting 3.0 in production, since other breaking changes may exist beyond the version constraints.
- How do I test sortable ordering in Laravel with PHPUnit or Pest?
- Create several records with known positions in a test, call the move helper, and assert the resulting order with database assertions or by re-querying the models. Use RefreshDatabase or DatabaseTransactions so position values do not leak between tests. Also test edge cases like moving the first item up or the last item down, where no reordering should occur.
- Will moving records with laravel-sortable perform well on large tables?
- Each move rewrites the position values of the affected rows, so very large tables can see noticeable write amplification if you reorder frequently. Add an index on the position column to keep ordering queries fast, and benchmark the moves against realistic data volumes. If you routinely reorder thousands of rows, consider whether a sparse or gap-based position scheme fits better than contiguous integers.
- What are the alternatives to akaunting/laravel-sortable for sortable Eloquent models?
- Spatie's eloquent-sortable is the most commonly cited alternative and offers similar trait-based ordering with group scoping. You can also implement a position column and reorder logic yourself if your requirements are simple. Choose based on whether you need grouped sorting, drag-and-drop before/after helpers, and compatibility with your Laravel and PHP version.