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: Listing states weight: 3

Say you have setup the invoice model as follows:

namespace App;

use App\States\Invoice\InvoiceState;
use App\States\Invoice\Declined;
use App\States\Invoice\Paid;
use App\States\Invoice\Pending;
use App\States\Fulfillment\FulfillmentState;
use App\States\Fulfillment\Complete;
use App\States\Fulfillment\Partial;
use App\States\Fulfillment\Unfulfilled;
use Illuminate\Database\Eloquent\Model;
use Spatie\ModelStates\HasStates;

class Invoice extends Model
{
    use HasStates;

    protected $casts = [
        'state' => InvoiceState::class,
        'fulfillment' => FulfillmentState::class,
    ];
}

Get Registered States

You can get all the registered states with Invoice::getStates(), which returns a collection of state morph names, grouped by column:

[
    "state" => [
        'declined',
        'paid',
        'pending',
    ],
    "fulfillment" => [
        'complete',
        'partial',
        'unfulfilled',
    ]
]

You can also get the registered states for a specific column with Invoice::getStatesFor('state'), which returns a collection of state classes:

[
    'declined',
    'paid',
    'pending',
],

Get Default States

You can get all the default states with Invoice::getDefaultStates(), which returns a collection of state classes, keyed by column:

[
    "state" => 'App\States\Invoice\Pending',
    "fulfillment" => null,
]

You can also get the default state for a specific column with Invoice::getDefaultStateFor('state'), which returns:

'App\States\Invoice\Pending'
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