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 Livewire Wizard Laravel Package

spatie/laravel-livewire-wizard

Lightweight Livewire components for building multi-step wizards in Laravel. Define a wizard with an ordered list of step components, each with its own screen and Livewire logic, and guide users through checkout-style flows with ease.

View on GitHub
Deep Wiki
Context7

title: Accessing state weight: 4

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.
  • forStepClass(StepComponent::class): similar to forStep, but you can pass in the class name of the step component.
  • 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()->forStepClass(App\Livewire\DevliveryAddressStep::class);
$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(),
        ]);
    }
}
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
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
twbs/bootstrap4