All Projects → spatie → Eloquent Sortable

spatie / Eloquent Sortable

Licence: mit
Sortable behaviour for Eloquent models

Projects that are alternatives of or similar to Eloquent Sortable

Laravel Ban
Laravel Ban simplify blocking and banning Eloquent models.
Stars: ✭ 572 (-37.42%)
Mutual labels:  eloquent, laravel, trait
Laravel Ownership
Laravel Ownership simplify management of Eloquent model's owner.
Stars: ✭ 71 (-92.23%)
Mutual labels:  eloquent, laravel, trait
Befriended
Eloquent Befriended brings social media-like features like following, blocking and filtering content based on following or blocked models.
Stars: ✭ 596 (-34.79%)
Mutual labels:  eloquent, laravel, trait
Laravel Likeable
Rate Eloquent models with Likes and Dislikes in Laravel. Development moved to Laravel Love package!
Stars: ✭ 95 (-89.61%)
Mutual labels:  eloquent, laravel, trait
Laravel Imageup
Auto Image & file upload, resize and crop for Laravel eloquent model using Intervention image
Stars: ✭ 646 (-29.32%)
Mutual labels:  eloquent, laravel, trait
Schedule
Schedule is a package that helps tracking schedules for your models. If you have workers in a company, you can set schedules for them and see their availability though the time.
Stars: ✭ 155 (-83.04%)
Mutual labels:  eloquent, laravel, trait
Laracsv
A Laravel package to easily generate CSV files from Eloquent model
Stars: ✭ 583 (-36.21%)
Mutual labels:  eloquent, laravel
Laravel Schemaless Attributes
Add schemaless attributes to Eloquent models
Stars: ✭ 595 (-34.9%)
Mutual labels:  eloquent, laravel
Validating
Automatically validating Eloquent models for Laravel
Stars: ✭ 906 (-0.88%)
Mutual labels:  eloquent, laravel
Laravel Mongodb
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
Stars: ✭ 5,860 (+541.14%)
Mutual labels:  eloquent, laravel
Compoships
Multi-columns relationships for Laravel's Eloquent ORM
Stars: ✭ 537 (-41.25%)
Mutual labels:  eloquent, laravel
Laravel Translatable
A Laravel package for multilingual models
Stars: ✭ 624 (-31.73%)
Mutual labels:  eloquent, laravel
Laravel Friendships
This package gives Eloquent models the ability to manage their friendships.
Stars: ✭ 651 (-28.77%)
Mutual labels:  eloquent, laravel
Laravel Model States
State support for models
Stars: ✭ 559 (-38.84%)
Mutual labels:  eloquent, laravel
Laravel Mediable
Laravel-Mediable is a package for easily uploading and attaching media files to models with Laravel 5.
Stars: ✭ 541 (-40.81%)
Mutual labels:  eloquent, laravel
Laravel Love
Add Social Reactions to Laravel Eloquent Models. It lets people express how they feel about the content. Fully customizable Weighted Reaction System & Reaction Type System with Like, Dislike and any other custom emotion types. Do you react?
Stars: ✭ 822 (-10.07%)
Mutual labels:  eloquent, laravel
Laravel
Laravel Model Generator
Stars: ✭ 715 (-21.77%)
Mutual labels:  eloquent, laravel
Eloquent Ldap
A Laravel 5.1 package that first tries to log the user against the internal database if that fails, it tries against the configured LDAP/AD server.
Stars: ✭ 19 (-97.92%)
Mutual labels:  eloquent, laravel
Laravel Stager
Laravel Stager State Machine, Its purpose is to add state machine functionality to models
Stars: ✭ 16 (-98.25%)
Mutual labels:  laravel, trait
Laravel Model Status
Easily add statuses to your models
Stars: ✭ 510 (-44.2%)
Mutual labels:  eloquent, laravel

Sortable behaviour for Eloquent models

Latest Version GitHub Workflow Status Software License Total Downloads

This package provides a trait that adds sortable behaviour to an Eloquent model.

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.

The package also provides a query scope to fetch all the records in the right order.

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

Support us

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.

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.

Installation

This package can be installed through Composer.

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:

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

Optionally you can publish the config file with:

php artisan vendor:publish --provider="Spatie\EloquentSortable\EloquentSortableServiceProvider" --tag="config"

This is the content of the file that will be published in config/eloquent-sortable.php

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,
];

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.

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,
    ];

    ...
}

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.

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

Assuming that the db-table for MyModel is empty:

$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

/**
 * 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.

/**
 * 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);

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

/**
 * 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.

/**
 * 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:

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

You can also move a model to the first or last position:

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

You can determine whether an element is first or last in order:

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

You can swap the order of two models:

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:

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

This will restrict the calculations to fields value of the model instance.

Tests

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

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

Alternatives

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].