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
08. Recap

Transcript

00:00
let's go ahead and do a quick recap just to reiterate why we've used enums here in place of either plain strings or enums in our database now over in our database here we know that the status can be one of a few pre-selected options we chose to not implement this at a database level
00:19
so we had an enum inside of our app which we could also use for things like validation so when we went ahead and stored a order we validated this against the order statuses that we had predefined in this backed enum it's a backed enum because each of these cases has values what we did then over on the form itself both on create and edit is used our enum values to iterate
00:46
through and build up the ui we did this in two places which means that our enum is a really good candidate already because it means that the more options that we add to this enum that's shown in the ui everywhere and it's also shown or validated everywhere as well so you can see here we've got a new value which we can create and that's perfectly valid to be put into the database
01:07
we then went ahead and implemented a label on our enum so we could show a nice ui value to the user rather than just relying on the single value itself we also then went over to the order model and we implemented a getter or an accessor for the status itself so when we tried to access the status from a model we didn't get back just a plain string value we got back an enum itself
01:34
which means that what we could have then done is gone ahead and used the label again when we're retrieving this value from the database so hopefully the course gave you a good insight into how to use enums in laravel if you have multiple values that could be inserted into your database columns

Episode summary

In this episode, we do a quick recap on why and how we've used enums in our Laravel app, rather than just plain strings or setting enum constraints directly in the database.

We started by defining an enum inside our application to represent order statuses. This helped us validate the data more reliably, since we could check that any status being saved matched one of our predefined enum cases. We used these enum values both on the create and edit forms, ensuring our UI always matched the valid options from our code. This makes it easy to update order statuses in the future — just update the enum, and both UI and validation stay in sync.

Next, we added a 'label' to our enum cases so we can display more user-friendly names in the UI, rather than just the enum value itself. To further integrate enums, we updated the Order model so that when we access the order status, we actually get an enum instance instead of just a string. That way, we can take advantage of things like getting the label directly from the model.

Overall, this recap should reinforce how enums help keep your code clean, consistent, and easier to maintain, especially when you have columns in your database with a set of limited, discrete values.

Episode discussion

No comments, yet. Be the first!