01. An enum example: Order statuses


If you're not already using enums for properties in your models that can have multiple values, then it's a really good idea to consider this and in this course we're going to get set up with exactly that and of course look at what enums are if you're not familiar
and then we're going to go through and just create out a few routes which do lots of different things like create data, go ahead and show data and we're going to see how enums are going to really really help us within predefined sets of values. So if you have no idea what I'm talking about we're going to go over to this fresh Laravel project. I've already created out a few
routes in here and obviously controllers, we'll take a look at them in a minute but we're going to go and create a migration here or rather a model and a migration and this is going to be an order model. Now if we think about an order model this could have multiple statuses so let's go over to the migration for this and I'm not going to fill in any data here we're just going
to have a string in here with a status so obviously you would have other data on here but we'll leave that off just so we can focus on this status. Now the status could have multiple values notice I've defined this as a string so really this status could contain any data at all so it could contain abc which wouldn't make much sense it could be ordered it could be shipped but really
what we want is a predefined list of statuses somewhere in our app that only can be chosen from that list so we don't want any additional text or any non-predefined text values to get into here. Now traditionally what we would do is we would use the enum type within our database and what this would allow us to do is set out an array of allowed values so for an order for
example we could have shipped we could have cancelled and then when we place this shipped value into the database this would be validated against the allowed list of items. Now we're not going to do that because there's a couple of downsides to this first of all these values are tied directly to our database they're not within our app we can't very easily extract them and then
use them in our app so that's the main downside of that so we're going to keep this as a loose string here so let's go ahead and just migrate the change here and then we're going to look at creating out an enum to represent this status and we do have a course on enums if you wanted to dive a little bit more deeply into or just go over and review once you're done. Now let's go over
to just our main app here it doesn't really matter where we put this for now I'm just going to create an enums directory and I'm going to call this order status so this enum directly relates to the status of an order it doesn't matter what you call the enum but that's what we are tying this down to so let's go ahead and give this a namespace that's now under app and enums and we're
not going to define a class here we're going to define obviously an enum and we'll just give this the same name as the thing that we have called it so it kind of looks like a class but what we need to do here is go ahead and make sure we specify a type for each of these backed things so this is a backed enum we're going to be defining it as and all that means is we have a case so this is whether
when the order is placed and we're going to go ahead and assign that the string placed it doesn't matter what this value is this is the value that's eventually going to end up inside of your database within that status column over here so if we just open up orders that's what's going to end up going into status so this could be anything this name could be anything it really doesn't matter what
you call it so let's just come up with a couple of other examples here so shipped maybe and of course let's change that to shipped and finally we'll say cancelled like so so we've got a few to play around with here so now that we've got our enum created we can start to use this every time we think about an order so when we're inserting data into an order when we're showing these options
maybe in an admin panel for a admin to select the status of an order or even just use these values in some kind of automated process when something gets shipped for example so let's head over to the next episode and we're going to start off by going over to this route here and looking at how we can iterate over the values of our enum and show them in a form ready to be created
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.