This episode is for members only

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

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


OK, so the first thing that we've got here is an order create controller. That's just returning a view here
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.
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
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.
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.
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.
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
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.
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.
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
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.
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,
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.
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.
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.
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.
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.
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.
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.
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.
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
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,
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.
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.
9 episodes 31 mins


Enums make working with multiple potential column values a breeze (think order statuses). Let's use an Enum in Laravel and cover storing, validating and accessing Enums.

By the end of the course, you'll be able to implement Enums in Laravel with ease, and make sure your multiple choice column data is strict and easy to globally update across your entire application.

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


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