Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

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
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Curotec logo

Curotec

World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.

Curotec
DreamzTech logo

DreamzTech

Hire 6-10+ Yrs. experienced skilled Laravel Developers from DreamzTech. We ensure NDA protected, 100% quality delivery. Contact Us & Discuss Your Need.

DreamzTech
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
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
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