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.

Episode summary

In this episode, we wrap things up by diving into our OrderIndexController to fetch and display a list of orders. We make use of the label method we set up earlier on our enum to show the order statuses in a clean way, without any extra fuss in the view.

After rendering out the orders using the enum's label, we take a look at the label function itself for a bit of quick refactoring. Since we're dealing with string-backed enums, we realize that our label method can be dramatically simplified — just a simple call to ucfirst() on the enum's value does the trick. If we had integer-backed enums, it would be a different story, but in this case, the refactor is straightforward and makes the code that little bit neater.

All in all, it's a short and sweet cleanup to make our enum-driven UI even easier to maintain!

Episode discussion

No comments, yet. Be the first!