In this episode, we dive into how to manage more complex state in our multi-step form, especially when we're updating existing products or need to keep track of information across steps (like a product we've already saved to the database). First, we review how state can start to get messy when it's spread across several steps, especially as you try to pass things like IDs or models between forms.
To simplify things, we introduce the idea of a custom state class. You'll see step by step how to set up a state directory, create a new class (like ProductState
), and wire it up to your multi-step wizard. This custom state lets us add handy helpers (like a product()
method) so that, no matter which step we're on, we can effortlessly grab the product model we're working with using something like this->state->product()
.
We walk through the necessary changes — creating the state class, ensuring it extends the right Laravel base class, fixing up namespaces, and updating our steps to make the most of this new shared state. You'll see how this approach makes it way simpler to fetch and update the product, e.g., attaching an image in one step or setting the published flag in another.
If your wizard needs to access and update a model across steps, custom state is the way to go! In the next episode, we'll build on this foundation by handling file uploads and attaching images to the product we've been working with.