Download the PHP package rutorika/sortable without Composer

On this page you can find all versions of the php package rutorika/sortable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

Please rate this library. Is it a good library?

All versions of sortable with dependencies

Informations about the package sortable

Build Status Scrutinizer Code Quality Latest Stable Version Total Downloads Latest Unstable Version License

Laravel 5 - Demo

https://github.com/boxfrommars/rutorika-sortable-demo5

Install

Install package through Composer

Version Compatibility

Laravel Rutorika Sortable
4 1.2.x (branch laravel4)
<=5.3 3.2.x
5.4 3.4.x
5.5 4.2.x
5.7 4.7.x
6.0 6.0.x
7.x, 8.x 8.x.x

Sortable Trait

Adds sortable behavior to Eloquent (Laravel) models

Usage

Add position field to your model (see below how to change this name):

Add \Rutorika\Sortable\SortableTrait to your Eloquent model.

if you want to use custom column name for position, set $sortableField:

Now you can move your entities with methods moveBefore($entity) and moveAfter($entity) (you dont need to save model after that, it has saved already):

Also this trait automatically defines entity position on the create event, so you do not need to add position manually, just create entities as usual:

This entity will be at position entitiesMaximumPosition + 1

To get ordered entities use the sorted scope:

Note : Resorting does not take place after a record is deleted. Gaps in positional values do not affect the ordering of your lists. However, if you prefer to prevent gaps you can reposition your models using the deleting event. Something like:

You need rutorika-sortable >=2.3 to use ->next()

Sortable groups

if you want group entity ordering by field, add to your model

now moving and ordering will be encapsulated by this field.

If you want group entity ordering by many fields, use as an array:

Sortable many to many

Let's assume your database structure is

and you want to order tags for each post

Add position column to the pivot table (you can use any name you want, but position is used by default)

Add \Rutorika\Sortable\BelongsToSortedManyTrait to your Post model and define belongsToSortedMany relation provided by this trait:

Note: $this->belongsToSortedMany has different signature then $this->belongsToMany -- the second argument for this method is $orderColumn ('position' by default), next arguments are the same

Attaching tags to post with save/sync/attach methods will set proper position

Getting related model is sorted by position

You can reorder tags for given post

Many to many demo: http://sortable5.boxfrommars.ru/posts (code)

You can also use polymorphic many to many relation with sortable behavour by using the MorphsToSortedManyTrait trait and returning $this->morphToSortedMany() from relation method.

By following the Laravel polymorphic many to many table relation your tables should look like

And your model like

Sortable Controller

Also this package provides \Rutorika\Sortable\SortableController, which handle requests to sort entities

Usage

Add the service provider to config/app.php

publish the config:

Add models you need to sort in the config config/sortable.php:

Add route to the sort method of the controller:

Now if you post to this route valid data:

Then entity with \Input::get('id') id will be moved relative by entity with \Input::get('positionEntityId') id.

For example, if request data is:

then the article with id 3 will be moved after the article with id 14.

jQuery UI sortable example

Note: Laravel 5 has csrf middleware enabled by default, so you should setup ajax requests: http://laravel.com/docs/5.0/routing#csrf-protection

Template

Template for many to many ordering

Development


Version
Requires php Version >=7.2.5
illuminate/support Version ^7.0|^8.0

The package rutorika/sortable contains the following files

Loading the files please wait ....