spatie / laravel-model-flags
为 Eloquent 模型添加标志
Requires
- php: ^8.0
- illuminate/contracts: ^9.0|^10.0|^11.0
- spatie/laravel-package-tools: ^1.13.6
Requires (Dev)
- laravel/pint: ^1.2
- nunomaduro/collision: ^6.3.1|^8.0
- orchestra/testbench: ^7.11|^8.0|^9.0
- pestphp/pest: ^1.22.1|^2.34
- pestphp/pest-plugin-laravel: ^1.3|^2.3
- phpunit/phpunit: ^9.5.25|^10.5
- spatie/laravel-ray: ^1.31
- spatie/test-time: ^1.3
README
为 Eloquent 模型添加标志
此包提供了一种特性,允许您向 Eloquent 模型添加标志。这些标志可以快速将进程状态、更新、迁移等保存到模型中,而无需使用迁移添加额外的列。
$user->hasFlag('receivedMail'); // returns false $user->flag('receivedMail'); // flag the user as having received the mail $user->hasFlag('receivedMail'); // returns true
它还提供了范围,以便快速查询具有特定标志的所有模型。
User::flagged('myFlag')->get(); // returns all models with the given flag User::notFlagged('myFlag')->get(); // returns all models without the given flag
尽管有其他用途,但此包的主要用途是轻松构建幂等(即可重启动)的代码片段。例如,当编写一个 Artisan 命令向每个用户发送邮件时。使用标志,您可以确保在命令取消(或失败)一半时,在第二次调用时,只会向尚未收到邮件的用户发送邮件。
// in an Artisan command User::notFlagged('wasSentPromotionMail') ->each(function(User $user) { Mail::to($user->email)->send(new PromotionMail()) $user->flag('wasSentPromotionMail'); }); });
无论您执行此命令多少次,用户都只会收到一次邮件。
支持我们
我们在创建 最佳开源包 上投入了大量的资源。您可以通过 购买我们的付费产品之一 来支持我们。
我们非常感谢您从家乡寄给我们明信片,注明您正在使用我们的哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将所有收到的明信片发布在 我们的虚拟明信片墙上。
安装
您可以通过 Composer 安装此包
composer require spatie/laravel-model-flags
在幕后,标志和与模型的关联将存储在 flags
表中。
要创建该 flags
表,您必须使用以下命令发布和运行迁移一次:
php artisan vendor:publish --tag="model-flags-migrations"
php artisan migrate
可选地,您可以使用以下命令发布配置文件:
php artisan vendor:publish --tag="model-flags-config"
这是发布配置文件的内容
return [ /* * The model used as the flag model. */ 'flag_model' => Spatie\ModelFlags\Models\Flag::class, ];
使用方法
要向模型添加可标志行为,只需让它使用 Spatie\ModelFlags\Models\Concerns\HasFlags
特性
use Illuminate\Database\Eloquent\Model; use Spatie\ModelFlags\Models\Concerns\HasFlags; class YourModel extends Model { use HasFlags; }
以下函数将可用。
// add a flag $model->flag('myFlag'); // returns true if the model has a flag with the given name $model->hasFlag('myFlag'); // remove a flag $model->unflag('myFlag'); // returns an array with the name of all flags on the model $model->flagNames(); // use the `flags` relation to delete all flags on a model $user->flags()->delete(); // use the `flags` relation to delete a particular flag on a model $user->flags()->where('name', 'myFlag')->delete();
标志只能对一个模型存在一次。当再次使用相同的标志标记模型时,标志的 updated_at
属性将被更新。
$model->flag('myFlag'); // after a while $model->flag('myFlag'); // update_at will be updated
您可以获得标志上次在模型上使用的时间。
$model->lastFlaggedAt(); // returns the update time of the lastly updated flag $model->lastFlaggedAt('myFlag') // returns the updated_at of the `myFlag` flag on the model $model->lastFlaggedAt('doesNotExist') // returns null if there is no flag with the given name
您还将获得以下范围
// query all models that have a flag with the given name YourModel::flagged('myFlag'); // query all models that have do not have a flag with the given name YourModel::notFlagged('myFlag');
要一次性从所有模型中删除标志,您可以使用 Spatie\ModelFlags\Models\Flag
模型删除标志。
use Spatie\ModelFlags\Models\Flag; // remove myFlag from all models Flag::where('name', 'myFlag')->delete();
测试
composer test
更新日志
请参阅 更新日志 了解最近更改的详细信息。
贡献
请参阅 贡献 了解详细信息。
安全漏洞
请参阅 我们的安全策略 了解如何报告安全漏洞。
鸣谢
特别感谢 Caneco 为标志设计的图标 ✨
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。