Position logic for Eloquent models with minimum setup
alvits/laravel-eloquent-position is a Laravel package for position logic for eloquent models with minimum setup.
It currently has 0 GitHub stars and 14 downloads on Packagist (latest version v1.0.5).
Install it with composer require alvits/laravel-eloquent-position.
Discover more Laravel packages by alvits
or browse all Laravel packages to compare alternatives.
Last updated
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.
Install via composer
composer require alvits/laravel-eloquent-position
position (can be custom) column in your table (model)PositionTrait into your model (if you are using custom column set the $positionColumn property)$positionGroup with the column name/names (supports single string or multiple columns)Then you can get your entries sorted:
// ASC
YourModel::sorted()->get()
// DESC
YourModel::sortedByDESC()->get()
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
public function up()
{
Schema::table('pages', function (Blueprint $table) {
$table->smallInteger('position')->default(0)->after('id');
});
// Update the order pages
Artisan::call('model:position', [
'model'=> \App\Models\Page\Page::class
]);
}
class Page extends Model
{
use PositionTrait;
public $table = 'pages';
public $positionGroup = ['parent_slug'];
protected $fillable = [
'title', 'slug', 'parent_slug', 'content', 'description', 'position'
];
}
You can listen to events for positioning changes. You can use the PositionEventsTrait for easy model registration.
....
class YourModel extends Model {
use PositionTrait, PositionEventsTrait;
....
}
Called before running the last position calculation and the final movement of other entries for given position.
Enables to:
Name: positioning
YourModel::positioning(function($model, $query) {
$query->query()->where('type', 'type'); // or etc
\Log::info('positioning', 'To '.$model->getPosition().' from '.$query->oldPosition());
});
Name: positioned
Example via trait:
YourModel::positioned(function($model) {
/// TODO
});
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.
php artisan model:position App\\Models\\YourModel
Uses the BasePositionTrait and PositionScopeTrait
You can set:
positionColumn to enable overriding for the position columndisablePositionUpdate disables the updated of other entriespositionGroup builds a filter from columns for position calculation. Supports single column or multiple columnsdefaultPositionValue allows returning different value when position is empty string or null. Default value is nullPositionScopeTraitPositionHelperTrait with (getLastUsedPosition, getNextPosition($position = null))See CONTRIBUTING.md for how to contribute changes. All contributions are welcome.
laravel-eloquent-position was written by Martin Kluska and is released under the MIT License.
Copyright (c) 2016 Martin Kluska