Download the PHP package iben12/laravel-statable without Composer
On this page you can find all versions of the php package iben12/laravel-statable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download iben12/laravel-statable
More information about iben12/laravel-statable
Files in iben12/laravel-statable
Package laravel-statable
Short Description Statable trait for Laravel Eloquent models
License MIT
Homepage http://github.com/iben12/laravel-statable
Informations about the package laravel-statable
Statable trait for Laravel Eloquent models
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.1
version explicitly. For Laravel below 7 require versionv1.3
.
Use composer to pull in the package:
Publish the database migration and state machine config:
Migrate the database:
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_state
in 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:
Now you have to edit the Post
model:
And that's it!
Usage
You can now access the following methods on your model:
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.
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:
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.