Accessing state | laravel-livewire-wizard | Spatie

   [Docs](https://spatie.be/docs)  [Laravel-livewire-wizard](https://spatie.be/docs/laravel-livewire-wizard/v1)  Usage  Accessing state

 Version   v2   v1

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

- [ Introduction ](https://spatie.be/docs/laravel-livewire-wizard/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-livewire-wizard/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-livewire-wizard/v1/requirements)
- [ Installation &amp; setup ](https://spatie.be/docs/laravel-livewire-wizard/v1/installation-setup)
- [ Questions and issues ](https://spatie.be/docs/laravel-livewire-wizard/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-livewire-wizard/v1/changelog)
- [ About us ](https://spatie.be/docs/laravel-livewire-wizard/v1/about-us)

Usage
-----

- [ Creating your first wizard ](https://spatie.be/docs/laravel-livewire-wizard/v1/usage/creating-your-first-wizard)
- [ Navigating steps ](https://spatie.be/docs/laravel-livewire-wizard/v1/usage/navigating-steps)
- [ Rendering navigation ](https://spatie.be/docs/laravel-livewire-wizard/v1/usage/rendering-navigation)
- [ Accessing state ](https://spatie.be/docs/laravel-livewire-wizard/v1/usage/accessing-state)
- [ Setting initial state ](https://spatie.be/docs/laravel-livewire-wizard/v1/usage/setting-initial-state)
- [ Testing wizards ](https://spatie.be/docs/laravel-livewire-wizard/v1/usage/testing-wizards)

      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-livewire-wizard                                                                                                                                                                                                                                    `

Accessing state
===============

###  On this page

1. [ Using a custom state object ](#content-using-a-custom-state-object)

In any of your step components you can access the state of the all other steps in the wizard. You can call the `state()` function in a step component, to get back an instance of `Spatie\LivewireWizard\Support\State`.

```
// in a step component

$this->state(); // returns instance of `Spatie\LivewireWizard\Support\State`
```

On that state object you can call these methods:

- `all()`: returns an array containing the values of all public properties of all steps in the wizard. The key of the items in the array is the name of the step. Optionally, you can pass is a key name to let the function only return the value for that key.
- `forStep($stepname)`: returns the values of all public properties of the given step.
- `currentStep()`: returns an array containing the values of all public properties of the current step. The result is almost identical to Livewire's native `all()` method, but with some internal properties filtered out.

```
// in a step component

$this->state()->all(); // returns all state from all steps in the wizard
$this->state()->forStep('delivery-address-step'); // returns all state of the given step
$this->state()->currentStep(); // returns all state of the current step
```

Using a custom state object
-----------------------------------------------------------------------------------------------------------------------------------------

By default, calling `$this->state()` in a step component will return an instance of `Spatie\LivewireWizard\Support\State`. Optionally, you can customize that class, so you can add little helper methods on your state class.

To get started, first create a class that extends `Spatie\LivewireWizard\Support\State`. You can add any method that you want on that custom class.

```
use Spatie\LivewireWizard\Support\State;

class MyCustomState extends State
{
    public function deliveryAddress(): array
    {
        $deliveryStepState = $this->forStep('delivery-address-step');

        return [
            'name' => $deliveryStepState['name'],
            'address' => $deliveryStepState['address'],
            'zip' => $deliveryStepState['zip'],
            'city' => $deliveryStepState['city'],
        ];
    }
}
```

Next, in you wizard class, you should add a method name `stateClass` and let it return the class name of your custom state.

```
use Spatie\LivewireWizard\Components\WizardComponent;

class CheckoutWizardComponent extends WizardComponent
{
    public function stateClass(): string
    {
        return MyCustomState::class;
    }
}
```

With this in place, the `state()` function of step components will return an instance of `MyCustomState`. You can use any custom method you added on your state class.

```
namespace App\Components;

use Spatie\LivewireWizard\Components\StepComponent;

class ConfirmOrderStepComponent extends StepComponent
{
    public function render()
    {
        return view('checkout-wizard.steps.confirm', [
            'address' => $this->state()->deliveryAddress(),
        ]);
    }
}
```
