This episode is for members only

Sign up to access "Enums in Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
02. Displaying enum cases

Transcript

00:00
OK, so the first thing that we've got here is an order create controller. That's just returning a view here
00:06
where we're going to go ahead and build our form. So let's just head over to Orders and Create. And sure enough, at the moment, we don't see anything. This view is actually empty at the moment.
00:17
So let's just fill this in with some information. And then we're going to pass our enum down so we can show these inside of a select, if we can get that to work, and actually show
00:28
the values that can potentially be created here. And then, of course, we're going to have a button in here to go ahead and submit that through and insert that into the database.
00:36
So let's just create our button that says Create. So this is going to go through two orders. The method here is going to be post. We're just keeping it really simple here.
00:45
And we want a list of options, like shipped, all of the options that we have inside of that enum. So how are we going to grab them? Well, let's go back over to our order create controller.
00:55
And down here, we can actually pass the statuses through. And let's go ahead and reference our order status enum. And then all we need to do here is use the cases method. Now, what this is going to do is give us an array of each
01:10
of them enums in here. So it's going to give us an enum that represents placed, an enum that represents shipped, and an enum that represents cancel.
01:20
So let's go and start to iterate through what we have here and show it in the form. So we're going to go ahead and, of course, create our a foreach loop here.
01:30
Let's say statuses as status. And let's wrap that in there. And we should be good to go. So here, let's just output the status itself
01:40
and just see what we get. And there we go. Sure enough, we've got placed, shipped, and canceled. As easy as that to grab these values.
01:48
They're now in one place. If we were to output these anywhere else, which we are going to be doing later on in the course, and we wanted to add a new order status,
01:56
it's just as easy to come over here, add it into one place, and have that reflected everywhere in your app. That's the main benefit of enums. You have a central place for these potential values.
02:07
And when you update them, as long as you're using that everywhere in your app to validate, to insert, to show somewhere, then you're always going to keep everything nice and up to date.
02:16
So for the value of this, let's go ahead and add in status. And let's go ahead and submit this through. Let's not forget our cross-site request forgery token. And we should be good.
02:26
So if we head over to our web routes again, we've got this order store controller. So I'm not going to do any validation just yet. We're just going to go ahead and create this out.
02:35
So let's go ahead and say order create. And then from the request, let's just grab the status. And then let's just return back here. So let's just pull the namespace in for order.
02:46
And we should be good. We should see this inserted into the database. So before we do that, if we head over to our order model, let's just make sure we set the status to be fillable.
03:00
And let's go over and try this out. So let's create an order with the type of placed. And yeah, we probably just forgot to add the select name in here.
03:11
So that needs to be status so it can get picked up. Let's try that one more time over in create. So create that. That looks like it's done.
03:19
There we go. And let's choose another value, shipped, create. And it's working nicely. Now, at the moment, we've got the benefit
03:27
of iterating over enums. But if I go ahead and inspect this and just change one of these over, let's just say ABC. Now, if I choose shipped and I create that,
03:40
sure enough, that's been inserted into the database. So clearly, the benefit of enums isn't shining at the moment because we can just go ahead and insert any of these values into our database table.
03:51
Before we look at validation, though, we're going to go over to a really important part of enums. And that is helper methods to allow us to show more appropriate values on the UI.

Episode summary

In this episode, we're working on building out a simple order creation form. We start with a blank page and a controller that's set up, but not doing much yet. The goal is to let users select an order status (like "placed," "shipped," or "canceled") from a <select> dropdown, and we'll use an enum for these statuses to keep our available options centralized and easy to maintain.

We walk through how to pass all the enum cases down to the view, using the cases method, and then iterate over those statuses in the form, making sure our dropdown always reflects the underlying enum. That means any new status you add to your enum will immediately show up in the form too. After that, we wire up the form submission so that new orders can be created with their selected status, making sure the controller, route, and model all work together.

We also spot and fix a couple of common mistakes, like forgetting the name attribute on the select field and adding the fillable property to the model. Once all that's sorted, we test it out, and everything works! But, as we see, right now you could still submit invalid statuses—so enforcing enum values at the storage/validation level is something we'll look at next. Before that, though, the next step is making the form display more user-friendly values with enum helper methods.

Episode discussion

No comments, yet. Be the first!