Eloquent Sortable

spatie/eloquent-sortable image

Eloquent Sortable stats

Downloads
8.8M
Stars
1,272
Open Issues
0
Forks
128

View on GitHub →

Sortable behaviour for eloquent models

Spatie Eloquent Sortable

The Spatie Eloquent Sortable package is a Laravel tool that integrates seamlessly with Eloquent models to provide easy management of sortable entities. It allows developers to automatically handle the order of model entries in databases with minimal configuration.

Key Features

  • Sortable Trait: Add sortable behavior to Eloquent models with a simple trait.
  • Automatic Order Management: Automatically assigns the highest order number to new model instances.
  • Customizable Order Column: Allows specification of a custom column to be used for storing order.
  • Query Scope for Ordering: Includes a scope for retrieving models in their proper order.
  • Reordering Capabilities: Methods to adjust the order of model instances directly from the model (moveOrderUp, moveOrderDown, etc.).
  • Grouping Support: Facilitates sorting within grouped records, making it ideal for multi-tenant applications.

Installation

Install via Composer:

composer require spatie/eloquent-sortable

For Laravel versions below 5.5, manually register the service provider in your config/app.php:

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

Optionally, publish the configuration file:

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

Usage

Implement the Sortable interface and use the SortableTrait in your model:

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

Retrieve ordered records:

$orderedRecords = MyModel::ordered()->get();

Reorder records:

MyModel::setNewOrder([1, 2, 3]);

Control sorting within specific groups by overriding buildSortQuery method in your model:

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

Additional Tools and Support

  • Integration Tests: Includes tests to ensure reliable integration.
  • Changelog and Contributions: Active maintenance and detailed release notes available.

This package is part of a suite of open-source packages by Spatie, with continued development supported by their premium courses and products. For any further guidance, refer to the package's GitHub repository.

spatie photo

We create open source, digital products and courses for the developer community

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Eloquent Sortable Related Articles

Log User Activity in Your Laravel App with Activity Log v5  image

Log User Activity in Your Laravel App with Activity Log v5

Read article
Laravel Event Projector Released image

Laravel Event Projector Released

Read article
Tighten logo

Tighten

We help companies turn great ideas into amazing apps, products, and services.

Tighten
Securing Laravel logo

Securing Laravel

The essential security resource for Laravel devs, covering everything you need to keep your apps secure. Sign up to receive weekly security tips and monthly in depth articles, diving deep into security concepts you need to know!

Securing Laravel
LoadForge logo

LoadForge

Scalable load testing for web apps & APIs. Simulate real-world traffic and identify breaking points and performance limits with powerful, scalable load tests designed for Laravel.

LoadForge
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Honeybadger logo

Honeybadger

Simple developer-focused application monitoring for Laravel. Error tracking, log management, uptime monitoring, status pages, and more!

Honeybadger
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech