2017 © Pedro Peláez
 

library eloquent-sortable

Sortable behaviour for eloquent models

image

spatie/eloquent-sortable

Sortable behaviour for eloquent models

  • Thursday, February 8, 2018
  • by Spatie
  • Repository
  • 17 Watchers
  • 437 Stars
  • 204,667 Installations
  • PHP
  • 24 Dependents
  • 45 Suggesters
  • 56 Forks
  • 0 Open issues
  • 28 Versions
  • 18 % Grown

The README.md

Sortable behaviour for Eloquent models

Latest Version GitHub Workflow Status Software License Total Downloads, (*1)

This package provides a trait that adds sortable behaviour to an Eloquent model., (*2)

The value of the order column of a new record of a model is determined by the maximum value of the order column of all records of that model + 1., (*3)

The package also provides a query scope to fetch all the records in the right order., (*4)

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*5)

Support us

Learn how to create a package like this one, by watching our premium video course:, (*6)

Laravel Package training, (*7)

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*8)

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall., (*9)

Installation

For Laravel 6.x or PHP 7.x, use version 3.x of this package., (*10)

This package can be installed through Composer., (*11)

composer require spatie/eloquent-sortable

In Laravel 5.5 and above the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file:, (*12)

'providers' => [
    ...
    Spatie\EloquentSortable\EloquentSortableServiceProvider::class,
];

Optionally you can publish the config file with:, (*13)

php artisan vendor:publish --tag=eloquent-sortable-config

This is the content of the file that will be published in config/eloquent-sortable.php, (*14)

return [
  /*
   * The name of the column that will be used to sort models.
   */
  'order_column_name' => 'order_column',

  /*
   * Define if the models should sort when creating. When true, the package
   * will automatically assign the highest order number to a new model
   */
  'sort_when_creating' => true,

  /*
   * Define if the timestamps should be ignored when sorting.
   * When true, updated_at will not be updated when using setNewOrder
   */
  'ignore_timestamps' => false,
];

Usage

To add sortable behaviour to your model you must: 1. Implement the Spatie\EloquentSortable\Sortable interface. 2. Use the trait Spatie\EloquentSortable\SortableTrait. 3. Optionally specify which column will be used as the order column. The default is order_column., (*15)

Example

use Spatie\EloquentSortable\Sortable;
use Spatie\EloquentSortable\SortableTrait;

class MyModel extends Model implements Sortable
{
    use SortableTrait;

    public $sortable = [
        'order_column_name' => 'order_column',
        'sort_when_creating' => true,
    ];

    // ...
}

If you don't set a value $sortable['order_column_name'] the package will assume that your order column name will be named order_column., (*16)

If you don't set a value $sortable['sort_when_creating'] the package will automatically assign the highest order number to a new model;, (*17)

Assuming that the db-table for MyModel is empty:, (*18)

$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 1

$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 2

$myModel = new MyModel();
$myModel->save(); // order_column for this record will be set to 3


//the trait also provides the ordered query scope
$orderedRecords = MyModel::ordered()->get();

You can set a new order for all the records using the setNewOrder-method, (*19)

/**
 * the record for model id 3 will have order_column value 1
 * the record for model id 1 will have order_column value 2
 * the record for model id 2 will have order_column value 3
 */
MyModel::setNewOrder([3,1,2]);

Optionally you can pass the starting order number as the second argument., (*20)

/**
 * the record for model id 3 will have order_column value 11
 * the record for model id 1 will have order_column value 12
 * the record for model id 2 will have order_column value 13
 */
MyModel::setNewOrder([3,1,2], 10);

You can modify the query that will be executed by passing a closure as the fourth argument., (*21)

/**
 * the record for model id 3 will have order_column value 11
 * the record for model id 1 will have order_column value 12
 * the record for model id 2 will have order_column value 13
 */
MyModel::setNewOrder([3,1,2], 10, null, function($query) {
    $query->withoutGlobalScope(new ActiveScope);
});

To sort using a column other than the primary key, use the setNewOrderByCustomColumn-method., (*22)

/**
 * the record for model uuid '7a051131-d387-4276-bfda-e7c376099715' will have order_column value 1
 * the record for model uuid '40324562-c7ca-4c69-8018-aff81bff8c95' will have order_column value 2
 * the record for model uuid '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' will have order_column value 3
 */
MyModel::setNewOrderByCustomColumn('uuid', [
   '7a051131-d387-4276-bfda-e7c376099715',
   '40324562-c7ca-4c69-8018-aff81bff8c95',
   '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1'
]);

As with setNewOrder, setNewOrderByCustomColumn will also accept an optional starting order argument., (*23)

/**
 * the record for model uuid '7a051131-d387-4276-bfda-e7c376099715' will have order_column value 10
 * the record for model uuid '40324562-c7ca-4c69-8018-aff81bff8c95' will have order_column value 11
 * the record for model uuid '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' will have order_column value 12
 */
MyModel::setNewOrderByCustomColumn('uuid', [
   '7a051131-d387-4276-bfda-e7c376099715',
   '40324562-c7ca-4c69-8018-aff81bff8c95',
   '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1'
], 10);

You can also move a model up or down with these methods:, (*24)

$myModel->moveOrderDown();
$myModel->moveOrderUp();

You can also move a model to the first or last position:, (*25)

$myModel->moveToStart();
$myModel->moveToEnd();

You can determine whether an element is first or last in order:, (*26)

$myModel->isFirstInOrder();
$myModel->isLastInOrder();

You can swap the order of two models:, (*27)

MyModel::swapOrder($myModel, $anotherModel);

Grouping

If your model/table has a grouping field (usually a foreign key): id,user_id, title, order_column and you'd like the above methods to take it into considerations, you can create a buildSortQuery method at your model:, (*28)

// MyModel.php

public function buildSortQuery()
{
    return static::query()->where('user_id', $this->user_id);
}

This will restrict the calculations to fields value of the model instance., (*29)

Dispatched events

Once a sort has been completed, an event (Spatie\EloquentSortable\EloquentModelSortedEvent) is dispatched that you can listen for. This can be useful for running post-sorting logic such as clearing caches or other actions that need to be taken after a sort., (*30)

The event has an isFor helper which allows you to conveniently check the Eloquent class that has been sorted., (*31)

Below is an example of how you can listen for this event:, (*32)

use Spatie\EloquentSortable\EloquentModelSortedEvent as SortEvent;

class SortingListener
{
    public function handle(SortEvent $event): void {
        if ($event->isFor(MyClass::class)) {
            // ToDo: flush our cache
        }
    }
}

Tests

The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit., (*33)

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information on what has changed recently., (*34)

Contributing

Please see CONTRIBUTING for details., (*35)

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities., (*36)

Credits

Alternatives

License

The MIT License (MIT). Please see License File for more information., (*37)

The Versions

08/02 2018

dev-master

9999999-dev https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

08/02 2018

3.4.2

3.4.2.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

16/04 2017

3.3.0

3.3.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

23/01 2017

3.2.1

3.2.1.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

23/01 2017

3.2.0

3.2.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

20/11 2016

3.1.0

3.1.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

22/10 2016

3.0.0

3.0.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

19/10 2016

2.3.0

2.3.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

19/10 2016

2.2.0

2.2.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

21/03 2016

2.1.1

2.1.1.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

25/02 2016

2.1.0

2.1.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

10/08 2015

2.0.1

2.0.1.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

06/08 2015

2.0.0

2.0.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

11/06 2015

1.1.2

1.1.2.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

11/06 2015

1.1.1

1.1.1.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

11/06 2015

1.1.0

1.1.0.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

22/05 2015

1.0.2

1.0.2.0 https://github.com/spatie/eloquent-sortable

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

18/05 2015

1.0.1

1.0.1.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

13/02 2015

1.0.0

1.0.0.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

26/01 2015

0.2.2

0.2.2.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

14/10 2014

0.2.1

0.2.1.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

02/09 2014

0.1.4

0.1.4.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

16/07 2014

0.1.3

0.1.3.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

15/07 2014

0.1.2

0.1.2.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

15/07 2014

0.1.1

0.1.1.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour

15/07 2014

0.1.0

0.1.0.0

Sortable behaviour for eloquent models

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent model sortable sort behaviour