ip/sorter-bundle
A Symfony bundle that adds reusable sorting support to your application, letting you define and apply sortable fields with minimal configuration. It integrates with Doctrine to keep query sorting clean and consistent across controllers, services, and templates.
Start by installing the package and publishing its assets:
composer require <vendor>/<package>
php artisan vendor:publish --provider="<Vendor>\\<Package>\\<Package>ServiceProvider"
php artisan migrate
Then inspect the published config file first — it typically defines the category/supercategory models, the sort column name, and whether ordering is global or scoped per supercategory. Check the package migrations to confirm the schema: you should expect a sort (or similarly named) integer column and a nullable parent/supercategory foreign key.
Apply the package trait to your model (or use the provided model directly):
use <Vendor>\\<Package>\\Traits\\HasSortableCategories;
class Category extends Model
{
use HasSortableCategories;
}
Your first real use case is usually an admin-managed category tree: create a few supercategories, attach child categories, and render them in stored order. Seed a small fixture and verify that reordering a child updates only its siblings within the same supercategory. That exercise surfaces most configuration problems early.
Scoped ordering. Most packages like this scope the sort value to the parent/supercategory. When listing categories, always query through the relationship or apply the package scope rather than a raw orderBy('sort') across the whole table. A typical pattern:
$supercategory->children()->ordered()->get();
If the package does not expose an ordered scope, use the documented sort column explicitly and always constrain by parent.
Moving categories between supercategories. Treat a move as a multi-step operation: reassign the parent, let the package recalculate the sort value, then persist. The v0.6.1 fix specifically addresses cases where the sort value failed to update after a supercategory change, so wrap moves in a transaction and assert the resulting order in tests:
DB::transaction(function () use ($category, $newParent) {
$category->supercategory_id = $newParent->id;
$category->save();
// re-fetch and assert sort placement
});
Admin reordering. For drag-and-drop interfaces, send the ordered list of sibling IDs and update sort values in a single transaction. Avoid issuing one HTTP request per row; a bulk reorder endpoint that accepts an array is easier to lock and validate.
Tree rendering. Eager-load children recursively where the package supports it, or load a flat ordered collection and build the tree in memory. For navigation menus, cache the serialized tree and invalidate on create, update, delete, and move events.
API resources. Expose id, name, supercategory_id, and sort in your API resource. If clients can reorder, expose a dedicated reorder endpoint rather than allowing arbitrary sort writes through mass assignment.
sort = 1 is unique across the table. Queries that ignore the supercategory will appear to have duplicate or unstable ordering.lockForUpdate() on sibling rows is a cheap safeguard.supercategory_id is normal for root categories, but your ordering scope must handle null parents consistently.sort and supercategory_id to be freely fillable from public requests; route those changes through dedicated actions.updateQuietly() or raw queries that bypass model events.How can I help you explore Laravel packages today?