Laravel and PHP Refactor: If-Else to Switch or Match

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i'm answering an email from one of you guys or in fact i answered privately but also decided to make a public video out of that a short video discussing an exact situation so i was asked to refactor this code and it's not really seen well so i've pasted it into carbon.now.sh and this is the code so it's probably a controller with repeating if statements and how to make it shorter beside the fact that i don't know why the person is doing echo in the controller and then exit because it's kind of pointless because you would probably get that variable and pass it to the view to the blade somewhere or maybe return as api so echo and exit is kind of pointless but that's not the point of this video the point of this video is if request this then we use one model like coupon otherwise we use another model otherwise we use another model so how to avoid that if else and make it shorter and i will show you two ways one will be with switch operator and php another one will be with match operator which came in php8 and also i will show you how to parse dynamic models and how to catch errors if something goes wrong so i've recreated a similar scenario not the exact same one but i've created a list controller with two types so there's a user model and a project model and depending on the request type in the get request in the url i load one or another and i've seated some data and here's the result so list with type of users and this returns users otherwise it returns projects and how to make it shorter of course first we can change the if to switch so for example switch request type and then case for every value so case of users we would define the model the model variable will be user otherwise it will be project so in case of projects model will be project and then we would be able to have records for example model as a variable then all since all of them use all statement and then echo records and exit again it's quite pointless to echo and exit but i will just follow the original code with the same logic now what is the model value in this case the model should be full path to the model which is app backslash backslash so one backslash is general backslash and another one is to escape that so app models user like this and similarly in this case we will have project app models project and of course with every case we have to have a break we could probably have a default statement but in the original code there was no default statement so i don't know what should happen if the request gift type is not one of the four mentioned ones so again let's leave the logic as it was originally and let's try it out so if we refresh list with type projects nothing changes it still works type users still works so we made it shorter with the switch so instead of if statement we have those two cases in the switch so that's already shorter now what if that model doesn't exist for example someone made a typo and put in users here this is an interesting scenario which i want to talk about so if we put users it will be an error of app models users not found but interesting part is that you cannot catch that with try catch exception so if you try to do try and then records and then catch general exception of php exception for example echo error or something that would not work so refresh and the error doesn't appear because it's not an exception it's a php error of class not found i have a separate video about errors and exceptions and i will link that in the description below but generally it's not an exception it's an error in php so that would be the correct try cash statement in this case and now for refresh we have an error actually it's hidden so let's do die dump error like this so we have an error here it's called but not as an exception it's an error so there's a difference but now if we go back to the switch case we can make it even shorter with php 8 match operator and the best way to follow new syntax for php 8 or recent php 8.1 it's stitcher io blog it's not even php documentation that i would recommend i recommend brand's blog on this topic he has a lot of articles on php 8 syntax and one of them is match or switch which puts everything in perspective one versus another directly so here's the switch statement and here's the match statement so we can change the switch here into model equals match of request type and then array array of users would be app models user let's fix the typo of users and then projects project like this and then we can remove or for now comment out the switch and let's try it out type users no error it still works type projects no error still works so match is even shorter than switch so you don't need break statements you don't need the case and it's even more readable so i do recommend to use match from php8 if you want more tips and free short videos like this one totally subscribe to the channel because i'm still putting daily videos and my goal is to reach 100 000 subscribers so you can help me with that by subscribing and telling your friends to subscribe and see you guys in other videos
Info
Channel: Laravel Daily
Views: 10,371
Rating: undefined out of 5
Keywords:
Id: sTm5P4IGD5g
Channel Id: undefined
Length: 5min 51sec (351 seconds)
Published: Sat Dec 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.