lassehaslev / laravel-sortable
Installs: 1 176
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Type:laravel-plugin
Requires (Dev)
- orchestra/testbench: 3.3.x
- phpunit/phpunit: ^5.6
This package is not auto-updated.
Last update: 2024-11-23 20:48:09 UTC
README
Add sorting logic to your eloquent models
Install
Run
composer require lassehaslev/laravel-sortable
Create your package and add the following line to providers
in config/app.php
LasseHaslev\LaravelSortable\Providers\ServiceProvider::class,
Usage
Include LasseHaslev\LaravelSortable\Traits\Sortable
to models to make it sortable
<? class TestObject extends Illuminate\Database\Eloquent\Model { use LasseHaslev\LaravelSortable\Traits\Sortable; }
You can also overwrite the column name that holds the sorting value
<? class TestObject extends Illuminate\Database\Eloquent\Model { use LasseHaslev\LaravelSortable\Traits\Sortable; protected $sortingColumnName = 'order'; // Default }
Api
// Get sorted list $sortedCollection = Object::sorted()->all(); // Move object to position Object::moveTo( $objectToMove, $position ); // Move to front Object::moveToFront( $objectToMove ); // Move to back Object::moveToBack( $objectToMove ); // Increment position by one Object::incrementPosition( $objectToMove ); // Decrement position by one Object::decrementPosition( $objectToMove );
Development
composer install yarn
Runing tests
# Run one time npm run test # Automaticly run test on changes npm run dev