laravel-israel/eloquent-status-mutator
Composer 安装命令:
composer require laravel-israel/eloquent-status-mutator
包简介
A status manager trait for Eloquent models
README 文档
README
Handling status changes of a model is always a pain.
Eloquent Status Mutator provides is a simple trait which enforces correct status changes & some more cool features.
Usage
Define Statuses
Define the statuses of the model in the statuses property:
class Order extends Model { use HasStatus; protected $statuses = [ 'opened' => [], 'paid' => ['from' => 'opened'], 'approved' => ['from' => 'paid'], 'shipped' => ['from' => ['paid', 'approved']], 'arrived' => ['from' => 'shipped'], 'cancelled' => ['not-from' => ['arrived']], ]; }
Automatic Status Enforcement
The package makes sure that only listed statuses can be set:
$order->status = 'opened'; // OK $order->status = 'some other status'; // Throws Exception
Status Flow Validation
The package enforces that status can be set only after defined statuses in its 'from' key
$order->status = 'opened'; $order->status = 'paid'; // OK $order->status = 'arrived'; // Throws Exception
The package also enforces the 'not-from' status
$order->status = 'arrived'; $order->status = 'cancelled'; // Throws Exception
Helpers
$order->status = 'paid'; if ($order->is('paid')) { echo 'The order is shipped'; } if ($order->canBe('shipped')) { echo 'The order can be shipped'; }
Before and After callbacks
In some cases, we need to do something after a specific status is set - for example, send a mail after an order is cancelled.
Our package invokes a method after status change by the convention of on + status name (camel cased)
class Order extends Model { use HasStatus; protected $statuses = [ 'opened' => [], 'paid' => ['from' => 'opened'], 'approved' => ['from' => 'paid'], 'shipped' => ['from' => ['paid', 'approved']], 'arrived' => ['from' => 'shipped'], 'cancelled' => ['not-from' => ['arrived']], ]; public function onCancelled() { // Send cancellation mail to the user } }
Installation
- Request the package via composer
composer require laravel-israel/eloquent-status-mutator
- Use
HasStatustrait in your model
class Order extends Model { use HasStatus; }
- Define the available statuses in the model
protected $statuses = [ 'opened' => [], 'paid' => ['from' => 'opened'], 'approved' => ['from' => 'paid'], 'shipped' => ['from' => ['paid', 'approved']], 'arrived' => ['from' => 'shipped'], 'cancelled' => ['not-from' => ['arrived']], ];
laravel-israel/eloquent-status-mutator 适用场景与选型建议
laravel-israel/eloquent-status-mutator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 4, 最近一次更新时间为 2017 年 09 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 laravel-israel/eloquent-status-mutator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 laravel-israel/eloquent-status-mutator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-09-24