Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Model States Laravel Package

spatie/laravel-model-states

Add robust state behavior to Laravel Eloquent models using the state pattern and state machines. Represent each state as a class, cast states transparently to/from the database, and define clear, safe transitions with configurable state logic.

View on GitHub
Deep Wiki
Context7

title: Retrieving transitionable states weight: 4

An array of transitionable states can be retrieved using the transitionableStates() on the state field.


abstract class PaymentState extends State
{
    // …

    public static function config(): StateConfig
    {
        return parent::config()
            ->allowTransition(Pending::class, Paid::class)
            ->allowTransition(Paid::class, Refunded::class);
    }
}
$transitionableStates = $payment->state->transitionableStates();

This will return an array with all transitionable states for the current state, for example Pending:

[
    0 => "paid"
]

Retrieving state instances

If you need the actual state instances instead of just their string representations, you can use the transitionableStateInstances() method:

$stateInstances = $payment->state->transitionableStateInstances();

This will return an array of instantiated state objects:

[
    0 => Paid {
        // State instance with model reference
    }
]

Simple example in Blade

This method is particularly useful when you need to access state methods directly. For example, to display available transitions with their properties:

[@foreach](https://github.com/foreach)($payment->state->transitionableStateInstances() as $stateInstance)
    <div>
        <span style="color: {{ $stateInstance->color() }}">{{ $stateInstance->label() }}</span>
        <i class="{{ $stateInstance->icon() }}"></i>
    </div>
[@endforeach](https://github.com/endforeach)

With this approach, you can directly call any method defined on your state classes, allowing you to encapsulate UI and business logic within your states:

abstract class PaymentState extends State
{
    abstract public function color(): string;
    abstract public function label(): string;
    abstract public function icon(): string;

    // ...other state methods
}

class Paid extends PaymentState
{
    public function color(): string
    {
        return '#4CAF50'; // green
    }

    public function label(): string
    {
        return 'Mark as Paid';
    }

    public function icon(): string
    {
        return 'check-circle';
    }
}

Retrieving state counts

This method tells you how many available transitions exist for the current state.

$stateCount = $payment->state->transitionableStatesCount(); // 4

Checking for available transitions

This method tells you whether there are any available transitions for the current state.

$hasTransitions = $payment->state->hasTransitionableStates(); // true or false

Can transition to

If you want to know whether a state can be transitioned to another one, you can use the canTransitionTo method:

$payment->state->canTransitionTo(Paid::class);
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport