ninoman/laravel-sortable

一个简单的包,用于给您的Laravel模型添加排序功能。

dev-master 2019-06-09 12:05 UTC

This package is auto-updated.

Last update: 2024-09-10 00:10:48 UTC


README

Software License

在许多情况下,您可能需要为模型添加排序功能。当然,每个人都需要一个简单的包来覆盖所有常见用例。

你知道吗? 你已经找到了它!

此包将自动为您新创建的模型应用排序索引,并处理所有重新排序操作。

安装

您可以使用composer安装此包。只需运行以下命令。

composer require ninoman/laravel-sortable

用法

使用此包非常简单。只需在您的模型中使用 Ninoman\LaravelSortable\Sortable trait,并在您的模型迁移中添加 sort_index 列,并将其添加到 $fillable 属性中。

use Ninoman\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;
    
    protected $fillable = [
        ...
        'sort_index',
        ...
    ];
    
    ...
}



class CreateMyModelsTable extends Migration
{
    public function up()
    {
        Schema::create('my_models', function (Blueprint $table) {
            ...
            $table->unsignedInteger('sort_index');
            ...
        });
    }

    public function down()
    {
        Schema::dropIfExists('my_models');
    }
}

但当然,每个酷炫的包都应该具有可配置性。这个也不例外 :)

配置

因此,如果您想更改排序列(默认为 sort_index),应在您的模型中设置 $sortIndexColumn 属性。

use Ninoman\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;
    
    public $sortIndexColumn = 'order';
    
    ...
}

如我上面所述,您新创建的模型将自动排序,但如果不希望这样做,您始终可以将属性 $setSortIndexOnCreating 设置为 false

use Ninoman\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;
    
    public $setSortIndexOnCreating = false;
    
    ...
}

让我们设想一个场景,当您有 用户,并且每个 用户 都有很多 帖子。在这种场景下,如果您想为 帖子 添加排序,将所有 帖子 一起排序将显得很奇怪,当然您会希望按每个用户(按 user_id 分组)进行排序。

这可以通过设置模型中 $sortingParentColumn 属性来完成,即您想要按其分组排序的列的名称。现在,您新创建的 帖子 将会为各自的用户独立排序。

use Ninoman\LaravelSortable\Sortable;

class Post extends Eloquent
{
    use Sortable;
    
    public $sortingParentColumn = 'user_id';
    
    ...
}

您还可以配置模型排序的起始索引,默认为1。要更改它,您应设置 $startSortingFrom 属性,即您想从哪个数字开始排序。

use Ninoman\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;
    
    public $startSortingFrom = 0;
    
    ...
}

MyModel::create([...]); // sort_index 0
MyModel::create([...]); // sort_index 1
MyModel::create([...]); // sort_index 2

有用功能

简单但有用的作用域

trait 还会为您的模型添加一些功能。例如,如果您想获取已排序的模型,只需在模型上应用 sorted 作用域。

/*
    Models 
    
    ['id' => 1, 'sort_index' => 2];
    ['id' => 2, 'sort_index' => 3];
    ['id' => 3, 'sort_index' => 1];
*/

MyModel::pluck('id'); // [1, 2, 3]
MyModel::sorted()->pluck('id'); // [3, 1, 2]

您还可以使用 sortedDesc 作用域,如你所料,它将以降序对模型进行排序。

方法

请确保这些方法会让您的生活更轻松。

如果您有两个模型并想交换它们,请使用 swapSort 方法

MyModel::swapSort($modelOne, $modelTwo);

为了操作单个模型的排序,您可以使用以下方法

$myModel->moveSortIndexDown();
$myModel->moveSortIndexUp();
$myModel->toSortingTop();
$myModel->toSortingBottom();

当然,您也可以直接更新负责排序索引的模型属性,其他所有实体将自动重新排序。

use Ninoman\LaravelSortable\Sortable;

class MyModel extends Eloquent
{
    use Sortable;
    
    public $sortIndexColumn = 'order';
    
    ...
}

$one = $myModel::create([...]); //order 1 
$two = $myModel::create([...]); //order 2 
$three = $myModel::create([...]); //order 3 
$four = $myModel::create([...]); //order 4

$four->update(['order' => 2]);
//$one -> order 1;
//$four -> order 2;
//$two -> order 3;
//$three -> order 4;
结论

这是一个轻量级、易于使用的包,您可以轻松地将其集成到您的应用程序中。请随时报告问题及可能的改进。

谢谢!