- How do I install and set up baril/orderly for a Laravel project?
- Run `composer require baril/orderly`, add the service provider to `config/app.php`, then add a `position` column (or custom name) to your table via a migration. Finally, use the `Orderable` trait in your Eloquent model and guard the position column.
- Does baril/orderly work with Laravel 12, or only older versions?
- Yes, baril/orderly supports Laravel 6 through 12 with version-specific releases. Check the [compatibility table](https://github.com/michaelbaril/orderly) to match your Laravel version with the correct package version.
- Can I use this package for grouped ordering (e.g., sorting items within categories)?
- Absolutely. baril/orderly supports grouped ordering by defining a `$groupColumn` in your model. This allows you to maintain separate order sequences for different categories or sections.
- What happens if I try to move a record without saving it first?
- The package will throw an error or silently fail, depending on your Laravel configuration. Always ensure the model is persisted before calling methods like `moveToOffset()` or `moveUp()` to avoid orphaned positions.
- Is there a way to optimize performance for large datasets (e.g., 50K+ rows)?
- For large datasets, add an index on the `position` column and consider batching updates. The package doesn’t natively support transactions for bulk operations, so you may need to wrap `saveOrder()` calls in a database transaction manually.
- How do I handle concurrent edits or race conditions in production?
- baril/orderly doesn’t include optimistic locking by default. To prevent race conditions, use database transactions or add a `version` column to your model and implement `SELECT ... FOR UPDATE` in critical paths.
- Can I use a custom column name instead of `position` for storing order?
- Yes, define a `$orderColumn` property in your model (e.g., `protected $orderColumn = 'sort_order';`). This is useful if `position` conflicts with existing schema or naming conventions.
- Does this package work with multi-database setups (e.g., MySQL + PostgreSQL)?
- Yes, baril/orderly is database-agnostic and supports MySQL, PostgreSQL, SQLite, and SQL Server (since v3.3.0). The same codebase works across databases without modifications.
- Is there a way to fix corrupted or missing position values in production?
- Yes, the package includes an Artisan command: `php artisan orderly:fix-positions`. This tool scans your table and regenerates positions, ensuring continuity after data corruption or manual deletions.
- What alternatives exist for ordering Eloquent models, and when should I consider them?
- Alternatives include custom solutions with a separate `order` table or packages like `spatie/laravel-activitylog` for hierarchical data. Use baril/orderly for simple drag-and-drop UIs; opt for alternatives if you need real-time updates (e.g., PostgreSQL LISTEN/NOTIFY) or complex hierarchies (e.g., tree structures).