2017 © Pedro Peláez
 

library eloquent-sortable

Sortable behaviour for eloquent models

image

johankladder/eloquent-sortable

Sortable behaviour for eloquent models

  • Thursday, March 8, 2018
  • by johankladder
  • Repository
  • 1 Watchers
  • 0 Stars
  • 875 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 57 Forks
  • 0 Open issues
  • 27 Versions
  • 1 % Grown

The README.md

Sortable behaviour for Eloquent models

Latest Version SensioLabsInsight Build Status Quality Score Software License StyleCI 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)

Installation

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

$ composer require spatie/eloquent-sortable

Usage

To add sortable behaviour to your model you must:
1. specify that the model will conform to Spatie\EloquentSortable\Sortable
2. use the trait Spatie\EloquentSortable\SortableTrait
3. specify which column will be used as the order column
, (*7)

Example

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

class MyModel extends Eloquent implements Sortable
{

    use SortableTrait;

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

    ...
}

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., (*8)

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

If you don't set a value $sortable['sort_by_group_column'] the package will automatically order by table order. If you specify this value it will sort only the rows which are set as column in the same table;, (*10)

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

$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, (*12)

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

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

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

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

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

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

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

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

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

Tests

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

$ vendor/bin/phpunit

Changelog

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

Contributing

Please see CONTRIBUTING for details., (*19)

Security

If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker., (*20)

Postcardware

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using., (*21)

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium., (*22)

We publish all received postcards on our company website., (*23)

Credits

Alternatives

Support us

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

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff., (*25)

License

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

The Versions

08/03 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

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