Configuring transitions | laravel-model-states | Spatie

 SPATIE

  Laravel Model States
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-model-states](https://spatie.be/docs/laravel-model-states/v1)  Working-with-transitions  Configuring transitions

 Version   v2   v1

 Other versions for crawler [v2](https://spatie.be/docs/laravel-model-states/v2) [v1](https://spatie.be/docs/laravel-model-states/v1)

  Configuring transitions
- [ Introduction ](https://spatie.be/docs/laravel-model-states/v1/01-introduction)
- [ Postcardware ](https://spatie.be/docs/laravel-model-states/v1/02-postcardware)
- [ Requirements ](https://spatie.be/docs/laravel-model-states/v1/03-requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-model-states/v1/04-installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-model-states/v1/05-questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-model-states/v1/06-changelog)
- [ About us ](https://spatie.be/docs/laravel-model-states/v1/07-about-us)

Working with states
-------------------

- [ Configuring states ](https://spatie.be/docs/laravel-model-states/v1/working-with-states/01-configuring-states)
- [ Serializing states ](https://spatie.be/docs/laravel-model-states/v1/working-with-states/02-serializing-states)
- [ Listing states ](https://spatie.be/docs/laravel-model-states/v1/working-with-states/03-listing-states)

Working with transitions
------------------------

- [ Configuring transitions ](https://spatie.be/docs/laravel-model-states/v1/working-with-transitions/01-configuring-transitions)
- [ Custom transition classes ](https://spatie.be/docs/laravel-model-states/v1/working-with-transitions/02-custom-transition-classes)
- [ Dependency injection in transition classes ](https://spatie.be/docs/laravel-model-states/v1/working-with-transitions/03-dependency-injection-in-transition-classes)
- [ Retrieving transitionable states ](https://spatie.be/docs/laravel-model-states/v1/working-with-transitions/04-retrieving-transitionable-states)

Querybuilder support
--------------------

- [ State scopes ](https://spatie.be/docs/laravel-model-states/v1/querybuilder-support/01-state-scopes)

Request validation
------------------

- [ State validation rule ](https://spatie.be/docs/laravel-model-states/v1/request-validation/01-state-validation-rule)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-model-states                                                                                                                                                                                                                                    `

Configuring transitions
=======================

###  On this page

1. [ Allow multiple transitions at once ](#content-allow-multiple-transitions-at-once)
2. [ Allowing multiple from states ](#content-allowing-multiple-from-states)
3. [ Using transitions ](#content-using-transitions)

Transitions can be used to transition the state of a model from one to another, in a structured and safe way.

You can specify which states are allowed to transition from one to another, and if you want to handle side effects or have more complex transitions, you can also provide custom transition classes.

Transitions are configured in the `registerStates` method on your model.

```
class Payment extends Model
{
    // …

    protected function registerStates(): void
    {
        $this->addState('state', PaymentState::class)
            ->allowTransition(Pending::class, Paid::class)
            ->allowTransition(Pending::class, Failed::class, PendingToFailed::class);
    }
}
```

In this example we're using both a simple transition, and a custom one. Transitions can be used like so:

```
$payment->state->transitionTo(Paid::class);
```

This line will only work when a valid transition was configured. If the initial state of `$payment` already was `Paid`, a `\Spatie\ModelStates\Exceptions\TransitionNotFound` will be thrown instead of changing the state.

Allow multiple transitions at once
--------------------------------------------------------------------------------------------------------------------------------------------------------------

A little shorthand `allowTransitions` can be used to allow multiple transitions at once:

```
class Payment extends Model
{
    // …

    protected function registerStates(): void
    {
        $this->addState('state', PaymentState::class)
            ->allowTransitions([
                [Pending::class, Paid::class],
                [Pending::class, Failed::class, PendingToFailed::class],
            ]);
    }
}
```

Allowing multiple from states
-----------------------------------------------------------------------------------------------------------------------------------------------

If you've got multiple states that can transition to the same state, you can define all of them in one `allowTransition` call:

```
class Payment extends Model
{
    // …

    protected function registerStates(): void
    {
        $this->addState('state', PaymentState::class)
            ->allowTransition([Created::class, Pending::class], Failed::class, ToFailed::class);
    }
}
```

Using transitions
-----------------------------------------------------------------------------------------------------------

Transitions can be used by calling the `transitionTo` method on the state field like so:

```
$payment->state->transitionTo(Paid::class);
```

If you only have one state field on your model, you can use the `transitionTo` method directly on it:

```
$payment->transitionTo(Paid::class);
```

If there are multiple fields, a `\Spatie\ModelStates\Exceptions\CouldNotResolveTransitionField` exception will be thrown. You can pass the state field name explicitly as a second parameter if you want to:

```
$payment->transitionTo(Paid::class, 'fieldName');
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
