This episode is for members only

Sign up to access "Livewire Multi Step Forms" right now.

Get started
Already a member? Sign in to continue
05. Storing and accessing state


OK, let's talk about how this package stores state. Now, a word of warning, this is going to get infinitely more complicated as we get to do things like upload images or upload files
in general. We'll talk about that in the practical example, but let's just look at a really basic example of how state is stored and accessed.
So what I'm going to do is head over to the description step component here. And once again, what I'm going to do is just die dump on the state for this package
as we saw before. So we saw an example of this when we were trying to figure out the name for this component, but let's go ahead and die dump on this
before we go to the final step and just see what happens. OK, so I'm going to say a title in here, hit Next Step, and a description in here, and hit Next Step. So that's going to die and dump all of our state.
Now, we've got this state class here. We can create our own state, which we're going to be doing later, but this will basically just store state automatically for you for each of the steps
and any of the public properties that you've got attached to any of your steps. So this can be really helpful, but it can also be pretty tricky when you're dealing
with more complex data. So let's go over to our title step and have a look in here. And you can see, sure enough, that has stored this state for us for that title
that we are entering into that input. And you guessed it, we have the same thing for the description as well. But this is really handy, because now in the final step,
what we can do is we can access this state, pull it out of each of the steps. And I'm going to show you a couple of ways to do that. And then we can just show it on the page.
And then, of course, in the final step, when we hit that Final Submit button, we can go ahead and take that state and put it into the database.
So let's go and look at how we access this state now, now that we can see that these properties here are automatically stored in state for us. So we're going to go over to the Publish step.
And we're going to do pretty much exactly the same thing. So we're going to have a Submit method in here. And for now, let's just die dump on our state so we can see all of our state.
And then we're going to head over to the title step component and move that over to the Publish step just here. Now, we can get rid of this input, because we're not going to have anything on there.
But in here, I'm just going to change this over to Publish. So this is where we're going to show all of our state before the user gets the chance to review it, hit Publish, and have this stored in the database.
So let's just run through this really quickly. A title, a description, and there we go. We'll show our state in here. We'll hit Publish, and we will create this.
Now, let's look at a couple of ways that we can access this state in our final Publish step. So we know that by just hitting Publish, we will get all of that state.
We'll have state for the Publish step, but obviously, we're not storing anything in here. OK, so first of all, let's go down to our render method, and let's output each of these items within this state.
And then we're going to kind of tidy this up by grabbing them all in one go and passing them through as an array. So we can pass this down to our component,
but also use this to create the record. So what you could do is pass the title down to your component here, and you could access the state, but you could do this for a specific step.
So you can say current step. You can say all, which will give you back all of the state, which is effectively what we're doing with this state object at the top here
in dying and dumping. So it's pretty straightforward. So let's go ahead and say for step, because we want to grab that for the title step specifically.
And we know what the name of that is. That's just going to follow the directory structure here. So it's going to be things, create, and then steps, and then titled step.
And then with this, we'll get back an array of that data. So we can just access the title from there. So we've got the title. Now we want to pass the description down.
And you probably noticed this is getting a little bit long and cumbersome. We're going to tidy this up in a second. And we're going to say description step
and description. And that's pretty much it. We've accessed the state from the first two steps, and now we've got that data in the publish step
to show on the page. So let's go over to our publish step. Let's create out an ordered list in here. We'll create out a list item, and we'll just
output the title in here like we normally would. And of course, we'll do the same for the description here as well. So let's just change this over to the description.
Great, so that should work nicely. We're accessing the title and the description for each of the steps, and then going ahead and showing them. So I'm just going to put anything in here.
And there we go. Great, we can review this data. We can hit publish. Now, the problem with this is we are doing this
by accessing each item as it goes. And that is a little bit annoying, particularly because we have these long names. But even if we didn't, this is a little bit annoying.
And we're going to have to repeat ourselves when we actually hit Submit to create the thing. So by that, I mean what we're going to have to do is take these two lines of code, and we're
going to say thing create, which we don't even have in our database yet, but we will do that. And then we will have to go ahead and pass this in. So it's a little bit annoying to grab all this and, of course,
do what we need. So before we go any further, let's go and just create out a really simple model so we can see this going into the database.
So let's create a model called thing with a migration. We'll head directly over to that. We know exactly the data that we need to store here. It's just going to be a title and a description.
We'll just keep this really simple with a title. And we'll just set the description to a string as well because it's not too much trouble. In fact, I am going to change that over to text.
And let's go ahead and migrate this. So phpArtisan, migrate, and we're done. So we can now create this thing, not forgetting to just go ahead and set our fillable or set guarded to false.
OK, so this is going to work. Let's just try it out. So abcdef and publish. There we go.
So if we head over to our database now, we have that in the database. But let's just tidy this up a little bit so it's a little bit more convenient to access.
Now, the first thing that jumps out at me is create out just a simple helper method in here, which will grab all of our state. So let's go ahead and create out a method in here
called stateToStore, whatever we want to say. And we're going to go ahead and grab this and return that as an array directly inside of here. And then what we can do is just pass that directly
into our model, so this stateToStore. And then down here, we can do the same thing. So we can just pass through our entire state as that array. So this stateToStore, and we're done.
We've tidied that up by putting all the state that we need to create this model into one method. This should work in exactly the same way. Let's try this out.
And yeah, of course, we need to change over our template now. So let's go to our publish step. And this is going to be now state and title and state and description.
OK, let's go over and try this one more time. And there we go. We see this. We hit Publish.
And of course, that gets created in the database. Now, if you wanted to be a little bit more fancy and tidy this up even further, let's say that you had a ton of fields,
you're not really going to want to duplicate these all the way down for each of your steps. It can get a little bit annoying. Basically, what we want to say is,
I just want to pluck out the title and description for all of our state. So just before we do that, let's take a look at what this stateAll gives us.
So we have this all method on here, which will give us all of our state. So again, the annoying thing about this is we have to go through each of the steps,
but let's just take a look. So in here, we've got the state for each of our steps. Now, this gives us an array with each of the names, which is really handy because all of these
are now encapsulated within each of the steps. But let's just say that for each of our steps, all of the items that we were storing were unique. If you had a title in your description step
and a title in your title step, the method we're about to use wouldn't work. So we'd have to have, through the whole form, every single property completely unique.
But usually, that's going to be the case. So what we could do here, instead of returning this huge array and manually extracting out each item, is we could go ahead and collect this up and do
something with LaravelCollections. So let's just go and create this as a variable, just temporarily, just so we can die and dump this. And let's see what we do.
So we're going to collect up all of the state. So let's say this stateAll. That gives us a LaravelCollection instance, which means we can start to filter through this,
map through it, flatten it, whatever we need to do. So we're going to go ahead and map through this with keys. That's really important. And for each of them items, we're
just going to return that item. Now, what this will essentially do is flatten this. So we end up with something that looks like this. Let's just go through our steps and have a look.
So this will basically merge everything from each of the steps into one. So that means that we get our title and we get our description.
So we don't really need to worry about these class names and all this stuff that's used internally, because we just want the data out. So we've got the title now, and we've
got the description in here. So now with this, what we can do is we can use the only method to just pluck out the title and the description.
So you would just pluck all the data from all of your steps out within this only method. You can pass an array in here as well, and we're done. Now, the really important part about this
is we want to cast it to an array, because we're going to be using it to insert into the database and also show in our template. So really, what we can now do is just return this.
And I'm going to go ahead and just comment this line out here. So that's our new way of just collecting up all of the data from all of our steps.
And that's a lot cleaner, because now when we add something else, we can just add another key in here, and it's going to be available to us.
So this shouldn't affect the way this works at all, because we're returning an array. So let's go and say abcdef. We still get that data in here.
We hit Publish, and sure enough, that has been created in the database as well. So although it's a little bit cumbersome with the state here, having the full name of each of the steps in here,
we can use tricks like this with Laravel Collections to clear that up. Or of course, if you did want to do something like this or you had some more complex data,
you can manually register your Livewire components to make this a little bit easier to manage. But that's basically everything that we need to know to continue on to our practical example
in the next section. This is going to get a little bit more complicated. But once you've learned the basics that we've covered here, you should understand everything we're going to do.
15 episodes1 hr 38 mins


Everything you need to build multi step forms in Livewire.

We'll cover the basics of creating a multi step form, adding steps, navigating steps and accessing state using the laravel-livewire-wizard package.

Finally, we'll build a fully working practical example with more advanced step navigation, file uploads, and custom state.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!


No comments, yet. Be the first to leave a comment.