Download the PHP package sudo520/laravel-sortable without Composer
On this page you can find all versions of the php package sudo520/laravel-sortable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-sortable
Make your Laravel models Sortable in a moment
There are lots of situations when you need a sorting functionality for your models. Of course everyone wants simple package to cover all common use cases.
Know what? You've found it!
This package will automatically apply sort index for your newly created models, and also handle all resorting stuff.
Installation
You can install this package using composer. Just run a command bellow.
Usage
It's very easy to start with this package. Just use Sudo520\LaravelSortable\Sortable
trait in your model, and add sort_index
column in your models migration and into $fillable
property.
But of course every cool package should be configurable. This one is too :)
Configurations
So if you want to change the sorting column (by default it is sort_index
), you should set a $sortIndexColumn
property in your model.
How I've mentioned above your newly created model will be sorted automatically, but in case you don't want it, you always can set property $setSortIndexOnCreating
to false
Let's imagine a situation, when you have Users and every user has many Posts. In this kind of situation if you would like to add sorting for Posts, it will be weird to sort all Posts together, of course you will want to sort them for each user (grouped by user_id
).
It can be easily done by setting $sortingParentColumn
property of your model name of the column by which you want to group your sorting. And your newly created Posts now will be sorted uniquely for their user.
You also can configure start index of your models sorting, by default it will start from 1. To change it you should set $startSortingFrom
property the number from which you want to start sorting.
Helpful stuff
Simple but useful scopes
Trait also will add some functionality to your models. For example, if you want get your models sorted, just apply sorted
scope on your models.
Also you can use sortedDesc
scope, which how you have guessed will order models in descending order.
Methods
Be sure this methods will make your life easier.
If you have two models and want to swap them use swapSort
method:
In order to manipulate your one model's sorting you can use those methods:
And of course you can just update your model's property, which is responsible for sort index and all other entities will be reordered automatically.
Conclusions
It's a lightweight, easy to use package which you can easily integrate into your application. Feel free to report about issues and possible improvements.
Thank you!