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 state and state machine behavior to Laravel Eloquent models. Represent each state as a class, automatically serialize to/from the database, and perform clean, explicit transitions with configurable rules—ideal for workflows like payments, orders, and approvals.

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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai