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.
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.
For Laravel 6.x or PHP 7.x, use version 3.x of this package.
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="eloquent-sortable-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,
];
To add sortable behaviour to your model you must:
Spatie\EloquentSortable\Sortable
interface.Spatie\EloquentSortable\SortableTrait
.order_column
.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);
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.
The package contains some integration/smoke tests, set up with Orchestra. The tests can be run via phpunit.
vendor/bin/phpunit
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.
ORM written in Typescript, inspired by Laravel Eloquent, supports Mongodb/Mongoose. Warning: This is a documentation for v0.4.x, if you are using v0.3.x please checkout readme for v0.3.x in here. If y
Eloquent-Sluggable Easy creation of slugs for your Eloquent models in Laravel. NOTE: These instructions are for the latest version of Laravel. If you are using an older version, please install a versi
简介 Eloquent 返回的所有多结果集都是 Illuminate\Database\Eloquent\Collection 对象的实例,包括通过 get 方法检索或通过访问关联关系获取到的结果。 Eloquent 的集合对象继承了 Laravel 的 集合基类,因此它自然也继承了数十种能优雅地处理 Eloquent 模型底层数组的方法。 当然,所有的集合都可以作为迭代器,你可以像遍历简单的 P
简介 数据库表通常相互关联。 例如,一篇博客文章可能有许多评论,或者一个订单对应一个下单用户。Eloquent 让这些关联的管理和使用变得简单,并支持多种类型的关联: 一对一 一对多 多对多 远程一对多 多态关联 多对多多态关联 定义关联 Eloquent 关联在 Eloquent 模型类中以方法的形式呈现。如同 Eloquent 模型本身,关联也可以作为强大的 查询语句构造器 使用,提供了强大的
简介 Laravel 的 Eloquent ORM 提供了漂亮、简洁的 ActiveRecord 实现来和数据库交互。每个数据库表都有一个对应的「模型」用来与该表交互。你可以通过模型查询数据表中的数据,并将新记录添加到数据表中。 在开始之前,请确保在 config/database.php 中配置数据库连接。更多关于数据库的配置信息,请查看 文档。 定义模型 首先,创建一个 Eloquent 模型
The Laravel magic you know, now applied to joins. Joins are very useful in a lot of ways. If you are here, you most likely know about and use them. Eloquent is very powerful, but it lacks a bit of the