PHPackages                             iben12/laravel-statable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. iben12/laravel-statable

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

iben12/laravel-statable
=======================

Statable trait for Laravel Eloquent models

v1.5.1(4y ago)96318.9k↑18.7%121MITPHPPHP ^7.3|^8.0CI failing

Since Nov 25Pushed 4y ago5 watchersCompare

[ Source](https://github.com/iben12/laravel-statable)[ Packagist](https://packagist.org/packages/iben12/laravel-statable)[ Docs](http://github.com/iben12/laravel-statable)[ RSS](/packages/iben12-laravel-statable/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (9)Dependencies (4)Versions (10)Used By (1)

Statable trait for Laravel Eloquent models
==========================================

[](#statable-trait-for-laravel-eloquent-models)

[![Run tests](https://github.com/iben12/laravel-statable/workflows/Run%20tests/badge.svg?event=push)](https://github.com/iben12/laravel-statable/workflows/Run%20tests/badge.svg?event=push)[![StyleCI](https://camo.githubusercontent.com/c14cd8930289f02a5b3c601ae0a9b69d7ad59e9eaaaaa05d8c56510885703039/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3135383933323837392f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/158932879)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/fb3132d761833f8ba734b53077e49badf5145450acb52cbea2969cd7f6611678/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6962656e31322f6c61726176656c2d7374617461626c652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/iben12/laravel-statable/?branch=master)

This trait provides drop-in functionality to manage state and state history of an existing Eloquent Model based on [winzou/state-machine](https://github.com/winzou/state-machine) using [sebdesign/laravel-state-machine](https://github.com/sebdesign/laravel-state-machine) service provider.

Installation
------------

[](#installation)

Compatibility:

VersionUpstreamLaravelPHP`v0.1``sebdesign/laravel-state-machine:^1.3`&lt; 5.5`v1.3``sebdesign/laravel-state-machine:^2.0`&gt;= 5.5`v1.4``sebdesign/laravel-state-machine:^3.0`&gt;= 7.0&gt;= 7.3`v1.5``sebdesign/laravel-state-machine:^3.2`&gt;= 7.0&gt;= 8.0> So if you are below Laravel 5.5, require `0.1` version explicitly. For Laravel below 7 require version `v1.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
-----

[](#usage)

#### Prerequisites

[](#prerequisites)

- Model class with some property holding state (we use `last_state` in the example)

#### Setup

[](#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

[](#usage-1)

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
-------------

[](#state-machine)

[sebdesign/laravel-state-machine](https://github.com/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](https://github.com/sebdesign/laravel-state-machine).

If you want to interact directly with the `StateMachine` object of your model, call `$model->stateMachine()`.

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

45

—

FairBetter than 91% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity50

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity72

Established project with proven stability

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~120 days

Recently: every ~126 days

Total

9

Last Release

1815d ago

Major Versions

v0.1 → v1.02019-09-25

PHP version history (3 changes)v0.1PHP ^7.0

v1.0PHP ^7.2

v1.5.0PHP ^7.3|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/9a8722d4a6bf459d5c10c2f5eb1887757f1cafda483073d7b10c11b8157acee8?d=identicon)[iben12](/maintainers/iben12)

---

Top Contributors

[![iben12](https://avatars.githubusercontent.com/u/2972954?v=4)](https://github.com/iben12 "iben12 (51 commits)")[![vpratfr](https://avatars.githubusercontent.com/u/2526465?v=4)](https://github.com/vpratfr "vpratfr (6 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (5 commits)")[![leonelngande](https://avatars.githubusercontent.com/u/13051253?v=4)](https://github.com/leonelngande "leonelngande (2 commits)")[![Skullbock](https://avatars.githubusercontent.com/u/1104083?v=4)](https://github.com/Skullbock "Skullbock (2 commits)")[![axklim](https://avatars.githubusercontent.com/u/12901433?v=4)](https://github.com/axklim "axklim (1 commits)")[![martindilling](https://avatars.githubusercontent.com/u/1018838?v=4)](https://github.com/martindilling "martindilling (1 commits)")

---

Tags

eventlaravelstatestatemachine

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/iben12-laravel-statable/health.svg)

```
[![Health](https://phpackages.com/badges/iben12-laravel-statable/health.svg)](https://phpackages.com/packages/iben12-laravel-statable)
```

###  Alternatives

[sebdesign/laravel-state-machine

Winzou State Machine service provider for Laravel

3431.4M1](/packages/sebdesign-laravel-state-machine)[winzou/state-machine

A very lightweight yet powerful PHP state machine

52414.0M19](/packages/winzou-state-machine)[yohang/finite

A simple PHP Finite State Machine

1.4k3.7M10](/packages/yohang-finite)[dougsisk/laravel-country-state

Country &amp; state helper for Laravel.

1731.9M](/packages/dougsisk-laravel-country-state)[gomachan46/state-machine

simple state machine with annotations for PHP, inspired by AASM known as a Ruby state machine.

1897.8k](/packages/gomachan46-state-machine)[proai/lumen-annotations

Route and event binding annotations for Laravel Lumen

1012.7k](/packages/proai-lumen-annotations)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
