9 Tips for Shorter Laravel Code

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys today i have a set of short tips how to make your laravel code shorter laravel is full of different helpers blade directors if statements and a lot of stuff that can help you to save a few symbols here and there a few if statements here and there and make your code shorter and more readable so let's start with number one blade check if the user is logged in or not and i see a lot of people doing something like if auth check or commented out if auth user check like that and then in here you show dashboard or login register right so in blade there are specific blade directories to avoid if auth check you can do just auth like this so auth else and auth and there's also an opposite directive so for example if user is not logged in and it is done so let's switch those so change this and instead of auth we write guest so if it's guest or not and then you have and guest to be honest else and and if also works so you can write and if or and guest also you can write else guest and that should all work refresh we didn't break anything tip number two is how to get logged in user data in the blade whenever you need them i see quite a lot of people especially junior developers pass the user from the controller something like this so get user like auth user and then pass that from the controller and then using auth you need to add the facade for use and this all works but instead of that in index blade auth user can be accessed like this with a helper auth user or you can use facade directly here of user it would also work let's try it out let's refresh so this is the name and the id of the user so that works my personal preference is to use helper just to avoid any errors of that auth facade being not initialized somewhere that helper is global so you can use it wherever in the blade in artisan commands in any class so it's a global thing and then with user id you can do something like this let's refresh still works but there's even shorter way shortcut for user id is auth id with brackets so it's not a field it's a function it checks the actual field of your user model auto increment which is id by default but maybe it's different maybe you overwrite it in the model so that function checks for it and returns the id number refresh we didn't break anything tip number three is about a for each loop in blade you don't need to check if the record list is empty like this so i see a lot of people doing that so if there is a count or is empty there's something so if not is empty or check for the record count in another way you don't need to do that instead of doing that so if else let's delete if and else and then change that for each to four else there's a special loop for else and then we change that end for each to empty so there's a different syntax so for each changes to for else with empty and for else like this let's reformat that and then you have one viewer if statement you have just for else which is the main loop and if it's empty so if the tasks is empty it shows this portion of the code tip number four is about initializing carbon instances with now or today so i see a lot of people doing carbon now in controllers or even in the blades and then use carbon like this laravel has a few helpers to avoid initializing that so in any place in any larval project in controller oil blade you can use just now function now is a helper of laravel which does all the initialization of carbon under the hood so if we refresh that list nothing should be broken still good but also there is now end today it works in almost the same way so if we refresh nothing changes but let's see dd today refresh and we have let's zoom it in so you have date with zeros if you need that so today's date but without the time and if you need now refresh ndd you also have the timestamp so today and now is roughly similar but a bit different tip number five is how to log some value instead of doing dd in the log so there is a function called info info is a shorter helper for log info and it will write all the value into laravel log file which is in storage logs laravel log i've made it empty and let's try it out refresh the page and the page is loading so you should use that in case of you need to debug that value but you don't want to stop the page like dd so if we open our lrv log again it contains the info with exactly what we need and in the info you can put anything like string object collection it should all work so you can put even more complex objects to the log for example let's do info tasks actually let's paginate by three so it's not too big refresh the page and we open laravel log and we see the object of the tasks and on the related note if you do want to use dd for tasks there's a shorter way so let's delete that one you probably would do something like dt tasks and refresh and you have dd right but do you know you can use dd on the collection so tasks created subdate paginate and then do dd at the very end so you don't need to write it on a new line and the result is the same and maybe even better because it shows the collection so dd is a laravel collection function which you can use on get dd or first dd or paginated tip number six is about a board function a board helper i see a lot of people doing the check some kind of check and if it is false then you return abort 4 3 or abort 404 or something like that so there are a few helpers that make it one liner in the laravel so there is a board if and then you pass in the condition inside of it and then what code should it abort with so something like that like this so one liner result is the same we refresh the page and we get the forbidden page and also there's a board unless which flips the situation flips the condition but i'm not a big fan personally of unless function there are a few more helpers with unless in laravel but i'm not a big fan because it's much harder to read about if it's kind of readable in almost english language and abort unless then you need to think what does it mean unless what is the condition so it's kind of a double negative so i wouldn't advise to use that but abort if is a great one-liner to avoid a separate if statement tip number seven is about hidden methods in forms for blade historically there was a function called csrf token and you would create something like this so underscore token is the actual field that you need for csrf protection but then with time laravel got shorter helpers so one shorter helper was field so then you don't need that input hidden and it would take care of that for you so csr field is the same but then at some point it got even shorter into blade director so instead of csrf field you can just do csrf and similarly if you have a put method form there was method field put so it creates a put method for the update form but a shorter way to do that is at method put something like this so there are csrf and method blade directives tip number eight is about encrypting the password if someone registered so for example i see a lot of people doing hash make from password and it all works and it actually is part of laravel breeze as i'm looking at it but there's a shorter way if you don't want to change encrypting algorithm hashing algorithm which is by default bcrypt then you can use bcrypt directly so there is a file called hash manager in the vendor folder in the core laravel and it gets the hashing mechanism the driver from config otherwise by default it's bcrypt and there is a special file config hashing.php which is not in vendor it's in config and default driver is bcrypt and there are other drivers supported but if you want to use default bcrypt instead of hashmake there is a helper called b crypt just like this and then you don't need to add use hash on top not sure if it's added yep so this one is not needed anymore and bcrypt is a shorter helper and you can use it wherever in the controller in artisan command or anywhere without using hashmake and tip number nine is about redirecting back to the previous url so quite often i see people doing something like store or update and then redirecting back like this or maybe there is a with message or something or with input and there's no need to do full redirect back there's a shorter way first let's see how it works we enter some task and we are redirected back to the same tasks create but there's a shorter way which i wanted to show you is just return back so there's a helper specific helper in laravel just back which does the same thing just in a shorter way so let's try if it all works we save the task and we are redirected back successfully so these are short tips about various parts of flowerville the things that i've learned over five plus years working with laravel and if you want more videos like this one and if you want to support me on this mission of shooting daily videos check out one of our free products from myself and my team laravel admin panel generator my courses which is 15 courses at the moment or live wire kit set of components and by purchasing one of them you support these daily videos for free see you guys in other videos
Info
Channel: Laravel Daily
Views: 28,450
Rating: undefined out of 5
Keywords:
Id: NUYqjdmsIj8
Channel Id: undefined
Length: 10min 16sec (616 seconds)
Published: Mon Mar 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.