Download the PHP package pion/laravel-eloquent-position without Composer
On this page you can find all versions of the php package pion/laravel-eloquent-position. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pion/laravel-eloquent-position
More information about pion/laravel-eloquent-position
Files in pion/laravel-eloquent-position
Package laravel-eloquent-position
Short Description Position logic for Eloquent models with minimum setup
License MIT
Informations about the package laravel-eloquent-position
Laravel eloquent model position
Position logic for Eloquent models with minimum setup. Before saving it will check if the position has changed and updates the other entries based on the models position value.
- Installation
- Usage
- Migration example
- Events
- Positioning
- Positioned
- Command
- Traits
- Changelog
- Todo
- Contribution
Installation
Tested in Laravel 5.4 - 8.
Install via composer
Usage
- Add a
position
(can be custom) column in your table (model) - Add
PositionTrait
into your model (if you are using custom column set the$positionColumn
property) - If you are using grouped entries (like parent_id and etc), you can set the
$positionGroup
with the column name/names (supports single string or multiple columns) - Add to form the position input (can be input[type=number] and etc) and fill/set the position on save
- When position is null or empty string, the last position will be used.
- If you are not using migration (the column exists), run the php artisian model:position` command to fix current entries (it will create correct order)
Then you can get your entries sorted:
If using default column name (position), the value will be converted to numeric value (if not null or empty string).
Get the position
Use the $model->getPosition()
or use the standard way by using the column name $model->position
Migration example
Model example
Events
You can listen to events for positioning changes. You can use the PositionEventsTrait
for easy model registration.
Positioning
Called before running the last position calculation and the final movement of other entries for given position.
Enables to:
- Restore the position to original value - return false
- Add additional query conditions via AbstractPositionQuery object in second parameter ($query->query() => Builder)
Name: positioning
Positioned
Name: positioned
Example via trait:
Command
Reposition command
This command will help you to fix the order of your models. You must provide a model class.
You must include the RecalculatePositionCommand
into your Console Kernel
class.
Traits
PositionTrait
Uses the BasePositionTrait
and PositionScopeTrait
You can set:
- string
positionColumn
to enable overriding for the position column - boolean
disablePositionUpdate
disables the updated of other entries - string|array
positionGroup
builds a filter from columns for position calculation. Supports single column or multiple columns - string
defaultPositionValue
allows returning different value when position is empty string or null. Default value is null
PositionScopeTrait
Todo
- [ ] Add the custom position trait to enable automatic convert to numeric value (don't want to use the setAttribute method) - In progress
- [ ] Add service provider for automatic command registration
- [ ] Add all docs for all features
- [ ] Add next/prev scope functions in
PositionScopeTrait
- [ ] Add
PositionHelperTrait
with (getLastUsedPosition, getNextPosition($position = null))
Contribution
See CONTRIBUTING.md for how to contribute changes. All contributions are welcome.
Copyright and License
laravel-eloquent-position was written by Martin Kluska and is released under the MIT License.
Copyright (c) 2016 Martin Kluska