oddvalue/eloquent-sortable

基于https://github.com/spatie/eloquent-sortable的附加功能

v2.0.0 2024-03-28 11:43 UTC

This package is auto-updated.

Last update: 2024-09-08 12:34:43 UTC


README

在Spatie的Eloquent Sortable基础上增加了以下附加功能

Latest Version on Packagist GitHub Tests Action Status Total Downloads Coverage

本包是Spatie的优秀Eloquent Sortable的扩展。它增加了以下附加功能

  • 新方法
    • $model->moveBefore(int|Sortable $target): void
    • $model->moveAfter(int|Sortable $target): void
    • $model->moveBetween(int|Sortable $before = null, Sortable $after = null): void
    • $model->moveTo(int|Sortable $newPosition): void

注意:移动方法会重建整个序列,因此不建议在非常大的数据集上使用此包。

安装

您可以通过composer安装此包

composer require oddvalue/eloquent-sortable

用法

实现接口并使用特质。

class MyModel extends Model implements \Oddvalue\EloquentSortable\Sortable
{
    use \Oddvalue\EloquentSortable\SortableTrait;
}
# New model automatically sorted to the end of the list upon creation
$model = MyModel::create(['title' => 'foo']);

# Move the model before the 5th item in the list
$model->moveBefore(MyModel::where('order_column', 5)->first());
// OR
$model->moveBefore(5);

# Move the model after the 5th item in the list
$model->moveAfter(MyModel::where('order_column', 5)->first());
// OR
$model->moveAfter(5);

# Move the model between the 5th and 6th item in the list
$model->moveBetween(
    MyModel::where('order_column', 5)->first(), 
    MyModel::where('order_column', 6)->first()
);
// OR
$model->moveBetween(5, 6);
# This is useful when using a javacript library that provides the node before
# and after the location an item is dropped

# Move the model to the 5th item in the list
$model->moveTo(MyModel::where('order_column', 5)->first());
// OR
$model->moveTo(5);

测试

composer test

变更日志

有关最近更改的更多信息,请参阅变更日志

贡献

有关详细信息,请参阅贡献指南

安全漏洞

有关报告安全漏洞的详细信息,请审查我们的安全策略

鸣谢

许可协议

MIT许可(MIT)。有关更多信息,请参阅许可文件