tonylaurent/laravel-eloquent-sequencer

一个允许您在 Eloquent 模型上创建和管理序列的包

dev-master 2022-08-08 14:59 UTC

This package is not auto-updated.

Last update: 2024-10-01 23:13:57 UTC


README

此包允许您为 Eloquent 模型创建和管理序列。

安装

通过 composer 安装此包

composer require tonylaurent/laravel-eloquent-sequencer

配置

要发布配置文件,请运行

php artisan vendor:publish --provider="TonyLaurent\LaravelEloquentSequencer\LaravelEloquentSequencerServiceProvider"

配置参数

您可以在 config/eloquentsequencer.php 中更改默认列名、初始值和序列化策略。

return [
    'column_name' => 'position',
    'initial_value' => 1,
    'strategy' => 'always',
];

strategy 配置决定了何时触发序列化,并接受以下值之一:alwayson_createon_updatenever

模型配置

$sequenceable 属性指定模型的序列列名

protected static $sequenceable = 'order';

将序列项分组在一起的关系键(键)

protected static $sequenceableKeys = [
    'task_list_id',
];

在上面的示例中,任务列表有多个任务。

对于 多态关系,指定两个关系键

protected static $sequenceableKeys = [
    'commentable_id',
    'commentable_type',
];

使用方法

在下面的示例中,任务列表可能有多个任务。

use TonyLaurent\LaravelEloquentSequencer\Traits\Sequenceable;
use Illuminate\Database\Eloquent\Model;

class Task extends Model
{
    use Sequenceable;

    protected $fillable = [
        'position',
    ];
    
    protected static $sequenceableKeys = [
        'task_list_id',
    ];

    public function taskList()
    {
        return $this->belongsTo(TaskList::class);
    }
}

创建一个对象

Task::create([
    'position' => 1,
    'task_list_id' => 1,
]);

如果为序列属性未提供值,则对象将被放置在序列的末尾。

Task::create(['task_list_id' => 1]);

更新和删除对象

序列中的其他项将被重新排列以保持序列的一致性。

$task->update(['position' => 4]);
$task->delete();

获取对象的序列值

$value = $task->getSequenceValue();

获取序列列名

$columnName = Task::getSequenceColumnName();

禁用自动序列化

$task->withoutSequencing()->update(['position' => 3]);
$task->withoutSequencing()->delete();

将查询范围限定为按序列值排序结果

$tasks = Task::sequenced()->get();

Artisan 命令

将序列值分配给所有其值设置为 null 的记录。

php artisan sequence:populate \\App\\Task

刷新模型的所有序列值。

php artisan sequence:flush \\App\\Task

测试

composer test

变更日志

请参阅 CHANGELOG 以获取有关最近更改的更多信息。

贡献

请参阅 CONTRIBUTING 以获取详细信息。

安全性

如果您发现任何安全相关的问题,请通过电子邮件 gustavorgentil@outlook.com 而不是使用问题跟踪器。

鸣谢

许可

MIT 许可证(MIT)。请参阅 许可文件 以获取更多信息。

Laravel 包模板

此包是使用 Laravel 包模板 生成的。