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: Custom default transition class weight: 6

When working with state transitions, you may need to pass additional contextual data to your StateChanged event listeners. While custom transitions allow this for specific state changes, sometimes you need this functionality for all transitions. To handle such scenarios DefaultTransition class can be extended.

The following example uses different logic depending on how transitionTo is called.

Creating custom default transition class:

use Spatie\ModelStates\DefaultTransition;
use Spatie\ModelStates\State;

class CustomDefaultTransitionWithAttributes extends DefaultTransition
{
    public function __construct($model, string $field, State $newState, public bool $silent = false)
    {
        parent::__construct($model, $field, $newState);
    }
}

Implement your state change listener to use the custom parameter:

use Spatie\ModelStates\Events\StateChanged;

class OrderStateChangedListener
{
    public function handle(StateChanged $event): void
    {
        $isSilent = $event->transition->silent;

        $this->processOrderState($event->model);

        if (! $isSilent) {
            $this->notifyUser($event->model);
        }
    }
}

Now we can pass additional parameter to transitionTo method, to omit notification logic:

class OrderService {
    public function markAsPaid(Order $order): void
    {
        // Will trigger notification
        $order->state->transitionTo(PaidState::class);
        // Also can be specified explicitly
        $order->state->transitionTo(PaidState::class, false);
    }

    public function markAsPaidSilently(Order $order): void
    {
        // Will not trigger notification
        $order->state->transitionTo(PaidState::class, true);
    }
}

Important notes:

  • Custom parameters are only available within the context of the event listeners
  • Parameters must be serializable if you plan to queue your state change listeners
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