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: Serializing states weight: 2

Say you create a Payment like so:

$payment = Payment::create();

If you've setup the default state to be Pending, this state field in the database will contain the class name of this state, eg. \App\States\Payment\Pending.

Chances are you don't want to work directly with a state's class name all the time. This is why you may add a static $name property on each state class, which will be used to serialize the state instead. Do not use a hyphen(-) as this will conflict with internal naming conventions.

class Paid extends PaymentState
{
    public static $name = 'paid';

    // …
}

You can still use ::class in your codebase though, the package will take care of name mappings for you.

For example:

$payment = Payment::create([
    'state' => Paid::class,
]);

The state value will still be saved as paid in the database.

Resolving states from the database

There's one caveat if you're using custom names: you'll need to make sure they can be resolved back from the database. In order to do so, the package requires you to keep the abstract state class and its concrete implementations together in the same directory, which allows them to be resolved automatically.

States/
  ├── Failed.php
  ├── Paid.php
  ├── PaymentState.php // This abstract class will automatically detect all relevant states within this directory.
  └── Pending.php
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