Retrieving transitionable states | 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/v2)  Working-with-transitions  Retrieving transitionable states

 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)

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

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

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

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

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

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

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

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

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

 Retrieving transitionable states
================================

###  On this page

1. [ Retrieving state instances ](#content-retrieving-state-instances)
2. [ Retrieving state counts ](#content-retrieving-state-counts)
3. [ Checking for available transitions ](#content-checking-for-available-transitions)
4. [ Can transition to ](#content-can-transition-to)

An array of transitionable states can be retrieved using the `transitionableStates()` on the state field.

```

abstract class PaymentState extends State
{
    // …

    public static function config(): StateConfig
    {
        return parent::config()
            ->allowTransition(Pending::class, Paid::class)
            ->allowTransition(Paid::class, Refunded::class);
    }
}
```

```
$transitionableStates = $payment->state->transitionableStates();
```

This will return an array with all transitionable states for the current state, for example `Pending`:

```
[
    0 => "paid"
]
```

Retrieving state instances
--------------------------------------------------------------------------------------------------------------------------------------

If you need the actual state instances instead of just their string representations, you can use the `transitionableStateInstances()` method:

```
$stateInstances = $payment->state->transitionableStateInstances();
```

This will return an array of instantiated state objects:

```
[
    0 => Paid {
        // State instance with model reference
    }
]
```

### Simple example in Blade

This method is particularly useful when you need to access state methods directly. For example, to display available transitions with their properties:

```
@foreach($payment->state->transitionableStateInstances() as $stateInstance)

        {{ $stateInstance->label() }}

@endforeach
```

With this approach, you can directly call any method defined on your state classes, allowing you to encapsulate UI and business logic within your states:

```
abstract class PaymentState extends State
{
    abstract public function color(): string;
    abstract public function label(): string;
    abstract public function icon(): string;

    // ...other state methods
}

class Paid extends PaymentState
{
    public function color(): string
    {
        return '#4CAF50'; // green
    }

    public function label(): string
    {
        return 'Mark as Paid';
    }

    public function icon(): string
    {
        return 'check-circle';
    }
}
```

Retrieving state counts
-----------------------------------------------------------------------------------------------------------------------------

This method tells you how many available transitions exist for the current state.

```
$stateCount = $payment->state->transitionableStatesCount(); // 4
```

Checking for available transitions
--------------------------------------------------------------------------------------------------------------------------------------------------------------

This method tells you whether there are any available transitions for the current state.

```
$hasTransitions = $payment->state->hasTransitionableStates(); // true or false
```

Can transition to
-----------------------------------------------------------------------------------------------------------

If you want to know whether a state can be transitioned to another one, you can use the `canTransitionTo` method:

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