Download the PHP package flyhjaelp/laravel-eloquent-orderable without Composer
On this page you can find all versions of the php package flyhjaelp/laravel-eloquent-orderable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download flyhjaelp/laravel-eloquent-orderable
More information about flyhjaelp/laravel-eloquent-orderable
Files in flyhjaelp/laravel-eloquent-orderable
Package laravel-eloquent-orderable
Short Description Make your Eloquent models orderable by using the orderable trait on them. Which automatically updates the order of all other models within it's group when the order of a current model is updated or a new model insereted or deleted.
License MIT
Informations about the package laravel-eloquent-orderable
Laravel Eloquent Orderable
Laravel Eloquent Orderable is a package that helps you make your eloquent models orderable, either within a group our within all other models of the same class.
Installation
Install via composer
Database setup
If you want to use the orderable functionality on a model it has to have a database column it can be ordered by. By default the package will look for a column named "order", but this can be overwritten. The order column should be an unsigned integer that's nullable. Example:
Default Usage
Creating a new model without a specified order
New instances will now have an order added to them, by default they are added as last in order.
Creating a new model with a specified order
If an order is specified when being created that order will update already existing orders accordingly.
Updating the order of a model
When updating a models order the other models automatically update their orders accordingly.
Deleting a model
When deleting a model, the order of all other models with a higher order will have their order decreased by one.
Grouping usage
You can make a group within your model, and the order only applies within the group. Example you might have a model called MenuItem which should be grouped by menu_id, and the order should only apply within it's group. To add a group to model you have to include the orderableWithinGroup trait and implement the following functions:
- scopeOrdered(Builder $query)
- scopeWithinOrderGroup(Builder $query, OrderableInterface $orderableModel)
- columnsAffectingOrderGroup()
Models are now ordered within their group
New instances will have an order added to them, by default they are added as last in order within their group
Usage within pivot models
You can make pivot models orderable if you wish to be able order a many to many relationship whenever it's retrieved. The order only works one way, meaning if you for example have a journey that has mutiple checkpoints you can make the checkpoints come in the correct order when you retrieve them from the journey, but not the other way around.
Setup pivot model ordering
For a pivot model to be orderable you have to use the PivotOrderable
trait on the model. It's also required to have an autoincrementing primary key(usually an "id") in the pivot relationship table. Besides that you have to implement the methods mentioned under OrderableWithinGroup
When calling the relationship from a model, you have to chain on the using
method on the belongsToMany
call when defining the relationship on the model. Also you have to add orderBy
method call if you want the relationship ordered when retrieved.
Overwriting default values
You have to change the default column used for storing the order in, as well as the default ordering scope.
Overwriting default ordering column
Overwriting global ordering scope
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
MIT