laravel-state-machines
State machines using Spatie Model States for complex state transitions. Use when working with complex state management, state transitions, or when user mentions state machines, Spatie Model States, state transitions, transition validation.
When & Why to Use This Skill
This Claude skill facilitates the implementation of robust state machines in Laravel applications using the Spatie Model States package. It assists developers in managing complex object lifecycles, defining valid state transitions, and encapsulating state-specific logic to ensure clean, maintainable, and type-safe codebases. By leveraging architectural patterns over simple enums, it helps prevent logic errors in sophisticated business workflows.
Use Cases
- E-commerce Order Management: Implementing complex order lifecycles (e.g., Draft to Pending to Shipped) with specific validation rules and side effects for each transition.
- Content Publishing Workflows: Managing article or document statuses such as 'Draft', 'Under Review', and 'Published', ensuring that only authorized transitions occur.
- Subscription Billing Systems: Handling intricate user account states like 'Trialing', 'Active', 'Past Due', and 'Canceled' while maintaining clean, state-specific behavior logic.
- Automated Approval Processes: Designing multi-step approval engines where the available actions and data requirements change dynamically based on the current state of the request.
| name | laravel-state-machines |
|---|---|
| description | State machines using Spatie Model States for complex state transitions. Use when working with complex state management, state transitions, or when user mentions state machines, Spatie Model States, state transitions, transition validation. |
Laravel State Machines
State machines with Spatie Model States for complex state transitions and validation.
Core Concept
state-management.md - State machine patterns:
- Spatie Model States setup
- State classes with behavior
- Transition classes with validation
- State-specific methods
- When to use state machines vs simple enums
- Testing state transitions
Pattern
// State classes
final class DraftOrderState extends OrderState
{
public function canBeSubmitted(): bool
{
return true;
}
}
final class PendingOrderState extends OrderState
{
public function canBeSubmitted(): bool
{
return false;
}
}
// Transition class
final class SubmitOrderTransition extends Transition
{
public function handle(): OrderState
{
// Validation and side effects
return new PendingOrderState($this->order);
}
}
// Usage
$order->status->transitionTo(PendingOrderState::class);
Use state machines for complex state with validation, side effects, or state-specific behavior. Use simple enums for basic status fields.