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
06. Using accessors to return enums

Transcript

00:00
If you think about it, the values that we have under status for each of our orders, we never really need to get just the string value back. We know that these are always going to match up to an enum.
00:11
So why don't we create an accessor on the model, which allows us to, when we access the status from our model, always return an enum.
00:19
Now, just to demo what I mean, if we head over to the edit controller, where we're showing the form, and we just died up on the order that we are trying to look at, if we give this a refresh, sure enough,
00:30
we get a string value back for canceled. Now, that's not entirely useful, because it would be great to have the enum in place of this, so we could perhaps call, if we remember,
00:42
over on the order status, the label if we ever needed that. And that's exactly what we're going to be doing in the next episode. But let's look at going over to our order model itself
00:51
and going ahead and creating an accessor, which returns an enum. So we're going to go ahead and create out a protected status function here.
00:59
And let's go ahead and type in the return value for this, which is going to be an attribute. And that comes from database and Eloquent. Let's go ahead and return attribute and make.
01:10
And then inside of here, what we can do is define out what happens when we access the status property. So basically, creating an accessor. So we can create a short closure here for this.
01:24
And that's going to give us back a value. So this is going to be the string value that we get back from the database. And we want to return something different here.
01:33
So just as an example, if you've not worked with accessors before in Eloquent, any time we access the status property now, that's going to go ahead and give us back ABC.
01:41
Not ideal, but let's just try this out. So let's say order status. And if we head over, there we go. We get ABC.
01:48
Now, what we want to be returned from here is an enum. The question is, how do we map up in our order status and return an enum based on the value here that we've got? Well, on an enum, we have a special try from method,
02:03
which allows us to grab this. So we can go ahead and say order status try from, and then just give the value. What that's going to do is it's going to return us an enum
02:14
representing the value that we have stored here. You can also say from if you like as well. But try from is going to go ahead and fail and just give a null value if this doesn't exist.
02:24
It shouldn't always exist in there, though. OK, let's try this out now. So if we go over to our order status or our edit controller and just dump on the order, let's give this a refresh.
02:36
Now, we've still got the status in here, but we've now got an accessor. So this time around, when we go ahead and call status, what we get back is an enum.
02:44
Perfect. Now what we can do from that, if we wanted to, is go ahead and grab the label for that, and we get back the nice UI value that we want to see.
02:53
And of course, if you had other methods in there, you could call them as well. So now that we've got that, that makes it really nice for us to go ahead and compare rather than using the raw value.
03:05
So let's just go over here, and we've got order of two. If we head over here, you can see that the status is canceled. This is now a breaking change because what we're technically
03:15
doing from that status is returning an enum, and we're trying to compare that to a string over in edit. So we've got order status, which is an enum, and we're trying to compare an enum to the string
03:26
value of an enum, which is just not going to work. What we can now do, though, is because order status is an enum, and we're iterating through enums, we can just compare two enums together.
03:38
That's going to tell us if it's the same enum, and therefore, the right one is going to be selected. So let's go over and give that a refresh, and there we go. We get the value back.
03:46
Now, you don't always have to do this. It's completely optional to create an accessor here, but I always find this really helpful for the reasons that we've just looked at.
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!