0082-laravel-state-machines

Manage Laravel model state transitions with Spatie Model States validation and side effects.

Updated Aug 23, 2026
One-click install
npx skills add https://github.com/MrJmpl3/codex_____data_____configuration --skill 0082-laravel-state-machines
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: 0082-laravel-state-machines
Source: https://github.com/MrJmpl3/codex_____data_____configuration/tree/main/skills/0082-laravel-state-machines
Command: npx skills add https://github.com/MrJmpl3/codex_____data_____configuration --skill 0082-laravel-state-machines

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

It solves the problem of managing complex Laravel model status flows where each transition needs validation and side effects, avoiding brittle if/else logic and inconsistent state changes.

Core Features & Use Cases

  • Type-safe state machines with Spatie Model States: Define states as classes and model the allowed transitions explicitly.
  • Transition classes with validation and side effects: Encapsulate rules, timestamps updates, and event dispatching inside dedicated transition handlers.
  • Clean model APIs for transitioning and guarding: Use markAs* methods to trigger transitions and canBe* methods to check whether a transition is allowed.
  • Use case: Implement an order lifecycle like pending -> processing -> completed (and optional cancelled) where each change updates fields (e.g., completed_at) and triggers domain events.

Quick Start

Review the transition rules and state classes in references/state-management.md, then adapt the OrderState hierarchy and Transition handlers to your domain and wire them into your model with Spatie’s HasStates.

Frequently Asked Questions about 0082-laravel-state-machines

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I manage complex Laravel state transitions without brittle if/else logic?

Complex Laravel state transitions are managed by defining states as classes using Spatie Model States, encapsulating rules inside dedicated transition handlers, and using model helper methods like `markAs*` and `canBe*` to trigger and guard changes.

How do I add validation and side effects to Laravel model state transitions?

Laravel model state transitions handle validation and side effects by encapsulating rules, timestamp updates, and domain event dispatching inside dedicated transition classes rather than scattering them across controllers or models.

What is the best way to implement an order lifecycle in Laravel?

An order lifecycle in Laravel is implemented by creating a state class hierarchy with explicit allowed transitions, such as pending to processing to completed, where each state change updates fields like `completed_at` and triggers domain events through transition handlers.

Can I use Spatie Model States to build type-safe workflows in Laravel?

Spatie Model States builds type-safe workflows by defining states as classes and modeling allowed transitions explicitly with attributes, ensuring model status changes only through well-defined and validated paths.

How do I check if a Laravel model state transition is allowed before applying it?

Checking if a Laravel model state transition is allowed is done using `canBe*` model helper methods, which verify whether a transition is valid before you trigger the actual state change with the corresponding `markAs*` method.

When should I use dedicated transition classes for Laravel model states?

Dedicated transition classes for Laravel model states should be used when status flows require explicit validation, timestamp updates, or domain event dispatching, ensuring transitions remain safe and consistent across complex workflows.