定制 bidzm/eloquent-state-machine 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bidzm/eloquent-state-machine

最新稳定版本:1.0.5

Composer 安装命令:

composer require bidzm/eloquent-state-machine

包简介

Eloquent State Machine

README 文档

README

Eloquent State Machine

Install through Composer

composer require bidzm/eloquent-state-machine 

Usage

Just add Bidzm\StateMachine to your eloquent model.

namespace App; use Illuminate\Database\Eloquent\Model; use Bidzm\StateMachine; class Message extends Model { use StateMachine; protected $fieldState = 'state'; // attribute name that use as state. Default `state`; protected $initialState = 'initiated'; // initial state. Default `initiated`; protected $transitions = [ [ 'from' => ['initiated'], // Previous state 'to' => 'queued', // State name 'on' => 'queue' // Action name ], [ 'from' => ['queued'], // Previous state 'to' => 'finished', // State name 'on' => 'finish' // Action name ], ]; } 

There some function that you can use

<?php $message = new App\Message(); $message->save(); $message->state; // `initiated` $message->can('queue'); // `true` // `canQueued` method on your model where `Queued` is the "studly" cased name of the state you want check $message->canQueued(); // `true` $message->canFinished(); // `false` $message->isState('initiated'); // `true` // `isInitiated` method on your model where `Initiated` is the "studly" cased name of the state you want check $message->isInitiated(); // `true` $message->isFinished(); // `false` $message->queue(); $message->state; // `queued` 

If you want doing some action in after or before transition fired. You must override method beforeTransition() or afterTransition()

<?php public function beforeTransition($from, $to) { // do something } public function afterTransition($from, $to) { // do something } 

State Change At

You can store when state changed, with attribute state_change_at in your table

namespace App; use Illuminate\Database\Eloquent\Model; use Bidzm\StateMachine; class Message extends Model { use StateMachine; protected $fieldState = 'state'; // Attribute name that use as state. Default `state`; protected $initialState = 'initiated'; // Initial state. Default `initiated`; protected $useChangeAt = true; // Determine if store time when state changed. Default false; protected $transitions = [ [ 'from' => ['initiated'], // Previous state 'to' => 'queued', // State name 'on' => 'queue' // Name action ], ]; } 

There some function that you can use

<?php $message = new App\Message(); $message->save(); $message->state; // `initiated` $message->initiatedAt(); // 2017-08-19T21:07:00+07:00 $message->queue(); $message->state; // `queued` $message->initiatedAt(); // 2017-08-19T21:07:00+07:00 $message->queuedAt(); // 2017-08-19T21:07:01+07:00 

License

MIT - http://opensource.org/licenses/MIT

统计信息

  • 总下载量: 19.69k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固