Transcription
Hello L friends. I'm sure you already have heard of enams in PHP and you probably are already using them in some way. They are fantastic.
But today I'd like to show you how Lava provides a ton of useful enam support directly in the framework. I'll give you a quick intro to enums again and then we'll look into casting, model binding, validation, cues, and much more. And all in combination with enums. You're going to love enums even more. I promise.
Throughout this video, we're going to work with this little demo application here where we have these posts here. And all these posts have a specific status like publish, archived, draft, and they also have tags here. We can also list all the blog articles for a specific tag or we can also create a new article and choose some specific tags and the status here. All right, so that's what we're working with today.
But let's start by giving us a little reminder of what enums are. And I'm going to start by just creating a new one. We have an artisant common for this. PHP artisan make enum. And I'm calling this post status. So the idea is for this video that we have this application. It's already working and we try to integrate some enums to yeah make this application a little bit better to use and to maintain. You're going to see why in just a bit. So post setters and now we can define which type. So we have pure enums and then we have enums with values. That's why they're called backed because they are backed with some values. And in our case, we want them to be um backed with a string. Okay, let's take a look. We already have here some other enums here, but this is the one which we just created. And this is what it looks like when you create a new enum. This is of the type string. And then we can define so-called cases. Um, yeah, draft is good. published all right and archived okay I'm good with that here so we have the case where we have the name of the enum and then we have the value attached to it and when we try to use this already post status let's go with draft one and you can see we get back here our first enum post status we can see the name and the value so this also means we can just access them like this which is cool which is the value and then the name and you've seen it already. We also have cases which is a method which all enums have and this is pretty cool because now you get all the different cases of this enums back and this is perfectly for iterating over all of your enum types and the enums are pretty cool because they define a collection of constants that you can use within your application. So now we have this one place where we define those strings here draft published and archived in this case and then we can use it throughout our application because without enums then we would have different places where we would have to define the strings we would have to check those strings and so on and this can become very hard to maintain. So enums are very powerful in this way and also they are typed. So this means we can check against this specific type here which is super cool and you're going to see some more example throughout this video.
But yeah, it doesn't end here by just defining those cases. We can also have methods here. So let's create a new one. This is called label. And we're going to return a string here. All right. Yes. And we want to use the match expression. And yes, that's exactly what we want to do. So now we are telling here that for every enum case here we want to define a label which is just a different representation of this string here just title case here in this case. So now this can mean this could be a string that we can use for um storing this enum in the database for example and this could be the representation of the enum on the front end. And now this is pretty cool because we can just call here the label method and we would get back the label. And this is super super useful and I hope if you're not familiar with enams you can already see why this is so cool. And let's add another example here where we have a color method. So now we're defining here colors for the specific enums types which we can enum cases which we then use on our front and you can see this will clean up your code quite a bit. And again we have this one place here where we define all of this which is really cool.
So let's go and check out how we can already make use of this post status inside our application. So we're going to start by I think it's the create controller here. Yeah. So we here we have defined that's currently how this application works. We have defined some tags and some statuses here which are just hardcoded in here. And again if we want to change this we need to change them here. That's one place. But of course we also have to check those values against in the blade file for our colors for example. So this means we already have multiple places where we would have to change and which we have to maintain for this application. So let's see how we can change this now. So instead of just providing those values here I would just call my enum and then we're going to use the cases method here which gives me back all of our enums. Okay, I'm providing this then to the few. And by the way, this is this page here where we can create new posts. So, but of course, we now have to change a few things because now what we're iterating over is not an array anymore. It's our enums. So, let's take a look where we're going to use our statuses. So, here we're iterating over all of our statuses, but this is now not a string anymore. It's an enum. So this means we have to change a few things. First for the value I want to take the value of the enum. And then here we're also checking against the value. So here we're just making sure if it's the same as the old then this should be selected. And then this is what we see. And here we want to use the label. I think this should work. Let's take a look. Okay, this looks good. You can see here we're now using the label. If I would change this to just a value, we should see that these are now lower case ydr. So let's change this back label which is a really cool use case to use one of those methods of an enum. Okay, cool.
But if we go down, you can see we now also have the same for text here. So let's maybe try to fix this as well inside our controller here for post text. I already have an enum here which is post tag and the method is not called all it's cases again. Okay, let's take a look at this one that's very similar to before. So these are all the text which we're using here. Level, PHP, front end, back end and AI. Again, this is a back enum with the type string and again we have a label method and we have a color method and we're going to I'm going to show you in a bit how we're going to use the color method. But here, let's go back down here where we're looping over all of our text again. Now, we want to use the value for the value of the input. And then here again, we have to check against the value as well. All right. And somewhere here, we don't need to make the first letter uppercase like this. We can just use our label method. And you can see this is still working. Yeah, it's looking good. Let's try this out. Level 13. Our next article, this will be a draft and it will be about level and PHP. And it looks like it's still working. Perfect. So, I hope you could already see how making use of our enums here is just super useful because it cleans up our not just our controller. We don't have those fixed values here anymore, but also our plate files a lot.
Okay, but let's go back to this view. So this is our index controller here. We're just providing our post with the user and then we have this index file here and this lot in here. [Music] Take a look. We're looping over here all of our posts. And yeah, you can see what I'm doing here is I'm here checking I want to find out what is the current class which I need for the current status. So pretty much the same which we're doing inside our enum. We're checking if the value is published. We want to use these Tailwind classes and then we're going to check if they are giving and then we're going to set those classes here which will give us our colored statuses here. Okay, but wouldn't it be cool if we could use our enum here as well? But yeah, currently what this returns is just a string which is stored in the database. So yeah, how can we connect our models, our migrations, our database tables with our enums? Let's take a look at our posts migration where we can see that status is just a string inside the database and for our text which are multiple items we use JSON. So you probably heard and you probably already have used also native enums field. So this is a little bit different here because now what we would have to do is yeah you can see it here. We would need to provide here all the values which then are stored directly in the database which is a different way to make sure that you only use a specific set of values for your database. But it has a very big disadvantage because it's very hard to maintain those values inside the database. And also if you want to reuse those values somewhere else except your database. This is also not really possible. That's why it's often better to just use a string like we had before, but we can still connect it to enum through our post model. Let me show you. And we're going to do this through casting here. So, we're already casting our text to an array. And now, let's add our status. And yeah, I want to cast it to enum. And we're just providing this enum here. And this is going to be enough in order to automatically translate the string to our enum object. Let's take a look. Um, let's find post and let's grab the status and we should get our enum back and yes we do. Okay. So now automatically leav eloquent does this for us casting the string value to an object and then we can directly use whatever we want because we get back our enum which is just so powerful. And by the way the same also works for our text. We can do this as well. So we're not casting this to an array anymore, but to an as enum. We have an array object or a collection. I'll go with a collection here. And then we have the off method. And here we're now providing our post tag class. And let's try this out as well. If we try to get our tags here, we get back a collection with all of the tags which we're using for this blog post. And again, this is super cool and super helpful.
Okay, let's take a look how can we refactor this now a bit more. So this means we can get rid of this whole statement here because the only thing that we need to do is we have our post, we have our status and then we have our color method and this should be enough to make it work. Oh no, it does not because or it does not because we already have to um change this also for text because this is currently failing. So here we're looping over all of our text in order to show the ones from the post. And again, we can get rid of all of this here. And a few things that we need to change. Yeah. So here we're going to use the value because this now is an enum. Again, we don't need to use this PHP method. We can just use our label method here. And then somewhere in here, we also have our classes here. And we can just go post. Oh, let me see. I think we're looping over the tags here. Yeah. Right. Okay. So, it's not post. It's directly tag. And then and then we can use our color method. Okay. Here we're using the label. Here we are using the value. I think we also have to change this for our status. Let's see where was this. Yeah. So, here we are. you want to show the label. And I think that's it. Let's give this a try. Yeah, I can see it's back working. We have all of our colors for our status, for our tags, and it's still working now. And yeah, it's now way cleaner because now we are defining all of this inside our enum, which is just again super super clean. And we have all of this together in one place. Just so cool. And I think we also have to fix this now for our tag pages. Yeah. So this is currently not working anymore. So let's take a look at our post tag controller. We are getting the string which is a tag from the route URL. So in this case it's front end. And then we're getting all the post where tags contain our tag. This is something that level provides out of the box. Pretty cool. And now inside here, let's see. We're looping over all of our posts. Yeah. First, we have our status here. We're using our label method. And then we're looping over the text. Again, we can get rid of all of this here. So, we had to use those classes here as well. But now, we can yeah, make this so much better. Our post tag, use our color method, and here again, we're we don't need this PHP method anymore. We're just going with the label. And I think that's it. Yep, we are back working here for our tag pages.
Okay, so cool already, but a few things that I still want to show you. So, we were talking about this post tag controller. Let me just dump out the tag here. So, you can see if I try to go one of the tag pages here, we can see here um we get the string PHP back. And that's because this is the string here inside the URL. And if we go to our post routes which I have here that's how we have defined this. We have post we have tags and then we get the the value whatever this is and we provide it to our controller and this is a string but it doesn't have to be a string. We can also use route model binding which you're used to doing something like this when you're using a user for example. So that's the name of the model. And we can do the same with enums as well. And the way that this works is we are just type hinting here that this should be a post tag. And let's again dump this out our tag here. And you should see that we already get our enum back. And we do it. It's just so easy. So that's implicit model binding which also works with enums. So this means here we need to change this to value. Okay, still working. But actually it also works like this. Let me show you. Yeah, so it's still working because in a lot of places in Lava, you can just use the enum. You don't have to provide the value and Laval is smart enough to check that this is an enum and that there is a string representative of it. And it just does this. And by the way, that's pretty cool. Um, let's get the status here again. And let's store this here in a variable status equals all right. So in PHP if you try to encode an enum what you get back is the value already and that's also how this works because in the back it's using JSON encode at some point and this will automatically translate your enum to the value which is just so cool that this works natively in PHP.
Okay, so far so good. We talked about enums in general, how to create them, cases, backed enums, methods that we can use. We talked about route model binding. We talked about casting. And yeah, we haven't talked about one thing yet, which is validation. So let's take a look at our store controller. Yes. So here we're doing some basic validation here for the request when we try to store a new post. So for the title for the status tax and yeah, since tax is an array we have here two line of rules. So we can also make this a little bit nicer because currently as you can see here we are validating the values we make sure that the values are only the ones which we want to allow. But of course, since we're now using an enum this is so much better. We can just use a rule illuminate validation rule enum and then provide our post status like this. Let me just copy this one here. And the same this stays the same but the same we can do inside here. So this should be now a post tag. I think that's it. I think the rest should be the same here. Yeah, let's let's give this a try. Hello bonds. Let's make this a draft. We want this to be about level PHP front ends. And yep, looks like it's still working. And as you can see, our rules are now first way cleaner. Then we also don't have the fixed values anymore because now all the values which they are checks against are inside our enum, which is pretty cool. So if we change it in the enum it's working everywhere as well which is yeah just so useful.
Yeah and I could just move on and show you more and more because there's really there really lot of places in level where enums are really integrated into the core of the framework and yeah we drive to give you the best experience while using enums. Let's maybe show you one more example. Let's go to the post store controller. Let's say we have this chop which we want to dispatch and we want to dispatch it on a specific queue. You can do it like this but you could also use now enum here and I think I have already one created. Yeah, it's called Q priority and we're going to use high here. So let's take a look at this. We also have an enum for our Q priorities. Again, whenever you would use strings in your application, you have a fixed amount of values, then an enum probably is a good use case here. And I've created one for our Q priority, high, default, and low. We also have a Q name, which makes it a little bit nicer. So, probably let's use the name method here instead. And here, there's so much more. Enums work perfectly in collection as well with a lot of collection methods. So there a lot of places for you to check out, but I hope I could already show you with this little example here how we could refactor this application to use just a couple of enums here. And yeah, this cleaned up our code quite a bit, especially on the front end, but also on the back end. And again, we have now these dedicated places with all information about one specific part of our application, like the status of a post. I really hope you just fell in love again with enums like I do every time while using them. We are working hard here at level to give you the best support and developer experience when working with them. So, please also give this video a like and subscribe to be the first to know about what is next in Lava. Bye. [Music]