In this episode, we dive into how state is stored and accessed when working with multi-step forms in our package.
We start by looking at the basics, like how entering data into different steps (for example, a title and then a description) gets automatically stored. We illustrate this by using some die-dump debugging, so you can see the state as you navigate the steps. You'll see that each step's public properties are automatically tracked for you by the package, which is super handy for basic forms, but it does get trickier as things get more complex.
Next, we explore how you can access and display the collected state when you get to the final "Publish" step. We'll walk through different ways to pull this state out for use in your UI or for saving it to the database. We show the manual way (grabbing each item by step and property) and then move on to a cleaner approach: using a helper method to gather and return all the necessary data as an array. This makes submitting everything much tidier.
To demonstrate saving data, we set up a really simple Thing
model with a migration and actually insert our stored form data into the database — showing the end-to-end process.
Since duplicating code to get each field can get messy (especially with more fields), we take things a step further and leverage Laravel Collections to cleanly gather all the state you need for saving and displaying. We break down how to flatten your state array, easily pluck out just the fields you want, and return everything as an array for storage or display.
By the end, you'll have a slick workflow for collecting, previewing, and saving multi-step form data, plus some handy tips for organizing your code as your form grows. This sets us up nicely for even more complex scenarios in the next section!