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
07. Refactoring the enum label method

Transcript

00:00
So to finish up, we're going to go over to our order index controller. We're going to pass down a list of all orders, and we're going to go ahead and output them with the label that we've implemented on our enum.
00:13
And then as a bonus, we're going to refactor the label function that we have inside of our enum or method that we have inside of our enum, just to make it a little bit shorter. Entirely optional, but with enum methods you can be really clever in the way that
00:26
you transform the data and show it out. So let's go ahead and just say order latest and get. So we just want to grab all orders, and let's head over to index.blade.php and just iterate through these. So let's just do this in the root here.
00:42
So for orders as order, and let's go ahead and end that for each. And then in here, let's output a div with hash and the order id. So order id, and then in here let's just output the status. So we know that status is now an enum,
01:02
so we can just go ahead and grab the label directly from the status. Rather than going in our view and doing something like string to upper or whatever we might do to transform this, we have all of the power of this now on the status itself. So if we head
01:16
over to here, there we go. We've got all of our statuses nicely output. Okay, so to finish up, let's go over to our order status and let's look at this label here. Entirely optional refactor, but this just shows you how we can make this
01:32
even simpler. So we're going to go ahead and just comment this out, and let's go ahead and re-implement this method in here. So label, that's going to return to us a string, and let's see what we can do here. So we could, if we think about it, with this
01:51
backed enum, each of these are already words that we would output on a UI. Sometimes with a backed enum you might have an int based enum, so you would have an integer in here for each one which is perfectly acceptable,
02:05
in which case you would have to do something like this to map them up properly. But in our case we've just got string values, so really the label here could just do a uppercase first on this, and remember this refers to each
02:22
individual enum value, and remember value represents each of these. So we've pretty much successfully refactored this down to a tiny tiny single line, and everything should still work nicely. Depending on the type of data that
02:37
you're working with though, of course something like this might not be possible, but it's always fun to try and refactor these methods down to make them as neat as possible. So there we go, a tiny refactor here but just makes that a little bit clearer.
9 episodes 31 mins

Overview

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!

Episode discussion

No comments, yet. Be the first!