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
03. Enum methods

Transcript

00:00
In terms of this form at the moment it's not too bad because we can clearly see what we're trying to
00:06
choose here in terms of the value. However if that's not the case and we had something that needed a little bit more transformation we can go ahead and create some sort of label for this. Now within an enum you can define lots of different methods so you can create our method in here and return something from it just like you would any other class. But the great thing here is that when
00:30
we're iterating through each of our enums this gives us back an enum that represents an order status. So what we can do is if we're iterating through and we're on the placed value or the placed enum we can call a method on that and get back a value we want to see for placed. So hopefully that makes sense but again we have a course on enums which go through all of this and
00:52
hopefully that will help you out. So what I'm going to do here is I'm going to create out a label method which returns a string that sort of matches up placed, shipped and cancelled and gives us a more UI friendly version of this value. So we're going to go ahead and just use a match here and we're going to pass this in. Remember this references the enum itself here so this could be
01:14
placed, shipped or cancelled and then we're going to go ahead and just match these up and return back a value. So we're going to say self placed and if that's the matched value to this enum that we're currently in we're going to go ahead and return placed with a capital P. So we're just going to make it a little bit more user friendly. We're also going to do the same for shipped here
01:36
so let's just change that over to shipped and we'll lastly finish up with cancelled and enter cancelled. So now what we can do is regardless of which one of these we're iterating through we can call this label method to get back the UI friendly version of this. So let's do that over in create.blade.php. Now this remember is an actual enum therefore it now has that label method
02:04
on that enum. So if we head over and give this a refresh sure enough we see a more friendly version of each one. There's loads you can do inside of enums but to be honest 90% of the time I just find myself defining the values out and then just creating a really simple method just to match these up to the values that I want to show in the UI.
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!

Comments

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