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
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Typesense Search logo

Typesense Search

Typesense is an open source, blazing-fast search engine, optimized for helping you build delightful search experiences for your sites and apps. Natively integrated with Laravel Scout.

Typesense Search
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
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
Celebian logo

Celebian

Celebian is a social media marketing agency specializing in helping their clients go viral on TikTok. Whether you're looking to reach a bigger audience or gain more Tiktok followers, likes, and views, they've got you covered.

Celebian
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum