Laravel Tips and Tricks

Status
Not open for further replies.

Andrew

Administrator
Staff member
Messages
27
Likes
6
Trophy Points
5
Followers
2
Joined
Mar 17, 2022
Starting today, I will be posting one to two Laravel refactoring tips a week. If this is what you are interested in, you can follow/watch this thread.

I will also make it sticky so that it is fixed at the top of the list under the Laravel (PHP Framework) forum.



Laravel Tip #1​

Here is an example of how we can use the abort_if method to make the code cleaner.

Also, In the end, I used the null safe operator that became available in PHP 8. It can also be used in the first condition if you prefer to stick with the if statement.

Laravel Refactoring Tip #1
 

Andrew

Administrator
Staff member
Messages
27
Likes
6
Trophy Points
5
Followers
2
Joined
Mar 17, 2022

Laravel Tip #2​

Here is an example of how to convert an array of paragraphs to HTML string with <p> tags.

I like to use this approach when working with fake data (factories) in Laravel.

How to convert array of paragraphs into HTML string.png
 

Andrew

Administrator
Staff member
Messages
27
Likes
6
Trophy Points
5
Followers
2
Joined
Mar 17, 2022

Laravel Tip #3​

Here are two ways to check the route name in Laravel. Personally, I prefer the second option using request()->routeIs($routeName).

Two ways of checking the route name.png
 

Andrew

Administrator
Staff member
Messages
27
Likes
6
Trophy Points
5
Followers
2
Joined
Mar 17, 2022

Laravel Tip #4​


Did you know about the $ loop variable in Laravel? This is an object that contains useful information about the current iteration of the loop.

Laravel $loop Object.png
 
Status
Not open for further replies.
Top