iben12/laravel-statable
Composer 安装命令:
composer require iben12/laravel-statable
包简介
Statable trait for Laravel Eloquent models
README 文档
README
This trait provides drop-in functionality to manage state and state history of an existing Eloquent Model based on winzou/state-machine using sebdesign/laravel-state-machine service provider.
Installation
Compatibility:
| Version | Upstream | Laravel | PHP |
|---|---|---|---|
v0.1 |
sebdesign/laravel-state-machine:^1.3 |
< 5.5 | |
v1.3 |
sebdesign/laravel-state-machine:^2.0 |
>= 5.5 | |
v1.4 |
sebdesign/laravel-state-machine:^3.0 |
>= 7.0 | >= 7.3 |
v1.5 |
sebdesign/laravel-state-machine:^3.2 |
>= 7.0 | >= 8.0 |
So if you are below Laravel 5.5, require
0.1version explicitly. For Laravel below 7 require versionv1.3.
Use composer to pull in the package:
$ composer require iben12/laravel-statable
Publish the database migration and state machine config:
$ php artisan vendor:publish --provider="Iben\Statable\ServiceProvider"
Migrate the database:
$ php artisan migrate
This migration creates the table for storing history of your models as a polymorphic relation.
Usage
Prerequisites
- Model class with some property holding state (we use
last_statein the example)
Setup
For this manual we will use a Post model as example.
First you configure the SM graph. Open config/state-machine.php and define a new graph:
return [ 'post' => [ 'class' => App\Post::class, 'graph' => 'post', 'property_path' => 'last_state', // should exist on model 'states' => [ 'draft', 'published', 'archived' ], 'transitions' => [ 'publish' => [ 'from' => ['draft'], 'to' => 'published' ], 'unpublish' => [ 'from' => ['published'], 'to' => 'draft' ], 'archive' => [ 'from' => ['draft', 'published'], 'to' => 'archived' ], 'unarchive' => [ 'from' => ['archived'], 'to' => 'draft' ] ], 'callbacks' => [ 'after' => [ 'history' => [ 'do' => 'StateHistoryManager@storeHistory' ] ] ] ] ]
Now you have to edit the Post model:
namespace App; use \Illuminate\Database\Eloquent\Model; use \Iben\Statable\Statable; class Post extends Model { use Statable; protected function getGraph() { return 'post'; // the SM config to use } }
And that's it!
Usage
You can now access the following methods on your model:
$post = \App\Post::first(); $post->last_state; // returns current state try { $post->apply('publish'); // applies transition } catch (\SM\SMException $e) { abort(500, $e->getMessage()); // if transition is not allowed, throws exception } $post->canApply('publish'); // returns boolean $post->stateHistory()->get(); // returns PostState collection for the given Post $post->stateHistory()->where('user_id', \Auth::id())->get(); // you can query history as any Eloquent relation
NOTE: The history saves the currently authenticated user, when applying a transition. This makes sense in most cases, but if you do not use the default Laravel authentication you can override the getActorId method to store the user with the history.
class Post extends Model { // ... public function getActorId() { // return id; } }
If the model is newly created (never been saved), so it does not have an id when applying
a transition, history will not be saved. If you want to be sure that all transitions
are saved in history, you can add this method to your model:
protected function saveBeforeTransition() { return true; }
State machine
sebdesign/laravel-state-machine provides a lot of features:
- using Gates and Policies
- Events
- callbacks for guards or other tasks
You can find the documentation in the repo.
If you want to interact directly with the StateMachine object of your model, call $model->stateMachine().
License
The MIT License (MIT). Please see License File for more information.
iben12/laravel-statable 适用场景与选型建议
iben12/laravel-statable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 315.59k 次下载、GitHub Stars 达 96, 最近一次更新时间为 2018 年 11 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「event」 「state」 「laravel」 「statemachine」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 iben12/laravel-statable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 iben12/laravel-statable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 iben12/laravel-statable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Doctrine implementation of the MetaborStd (Statemachine) for PHP 8.2+
A simple state dropdown field for SilverStripe forms
A state Machine library for business processes
Metamel Addresses is a polymorphic Laravel package, for address book management. You can add addresses to any eloquent model with ease.
Symfony bundle for broadway/broadway.
Convert and operate with FIPS codes for states, counties, etc.
统计信息
- 总下载量: 315.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 96
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-24