alejandrotrevi/laravel-ankal

为您的 Laravel 模型提供超级简单的状态管理

v1.0.1 2021-07-02 00:04 UTC

This package is auto-updated.

Last update: 2024-09-29 05:49:54 UTC


README

Latest Stable Version Total Downloads GitHub Actions

有时您只需要一种简单的方式来管理状态。

安装

您可以通过 composer 安装此包。

composer require alejandrotrevi/laravel-ankal

可选地,您可以发布库中包含的迁移。包含的迁移是向您的表添加所需列或从现有表中删除列的好地方。

php artisan vendor:publish --provider="Alejandrotrevi\LaravelAnkal\LaravelAnkalServiceProvider" --tag="migrations"

使用方法

HasStatuses 特性添加到模型中。

use Alejandrotrevi\LaravelAnkal\HasStatuses;

class MyModel extends Model
{
    use HasStatuses;
}

将必要的迁移添加到您将使用状态的每个表上。

Schema::create('my_table', function (Blueprint $table) {
    $table->statusColumns();
});

幕后操作基本上添加了3个列:statusreasonstatus_updated_at

可选地,您可以为该表设置默认状态,您只需将一个额外的参数传递给 statusColumns() 表修改器,这个额外的参数是创建新模型时 status 列将具有的默认状态。

Schema::create('my_table', function (Blueprint $table) {
    $table->statusColumns('my_default_status');
});

设置状态

您可以这样设置新的状态

$model->setStatus('status');

您还可以提供状态修改的原因。

$model->setStatus('status', 'why this status changed?');

由于状态位于同一张表中,您只需将状态作为模型上的另一个属性调用即可。

$model->status;
$model->reason;
$model->status_updated_at;

作用域

您有两个作用域可用于您的模型:currentStatusexceptStatus

// All models with status "status"
Model::currentStatus('status');

// All models with status "status" or "other-status"
Model::currentStatus('status', 'other-status');
Model::currentStatus(['status', 'other-status']);

无指定状态

// All models except those with the "my-status" status
Model::exceptStatus('my-status');

// All models except those with the "my-status" or "other-status" statuses.
Model::exceptStatus('my-status', 'other-status');
Model::exceptStatus(['my-status', 'other-status']);

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全

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

致谢

此包深受 spatie/laravel-model-status 包的启发,这个目标是 Spatie 解决方案的简化版本,所有荣誉都应归功于他们 🤗

许可

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

为什么是 Ankal?

Ankal 在玛雅语中意为“存在”。