Categories: Laravel Application, Laravel Framework - The PHP Framework For Web Artisans

Laravel Best Practices In 2019 For Your Application3 min read

Laravel Best Practices In 2019
Reading Time: 2 minutes

There are roughly 1,081,260 live websites which are using Laravel today but many of them are built without proper coding structure or they lacks important Laravel practices. In this post I am going to discuss some of the Laravel Best Practices that you can use to make your code more awesome and shorter. It is not the Laravel adaption of SOILD principles but some things that you should use in your application from now onwards.


Most of the Laravel developers put their validation logic into the controller which is a bad practice. You should put your validation rules into the Request Classes. For Example:-

Don’t repeat yourself (DRY)

One of the oldest rule in code writing that you should not repeat your code. Write Once , Use Everywhere. And that’s what you should be doing in Laravel as well. You must reuse Blade template , Eloquent classes etc wherever possible. For Example:-

Use Eloquent Instead Of Query Builder & Raw SQL Queries

Eloquent provides a beautiful, simple ActiveRecord implementation for working with your database. Using Eloquent you can write easy , maintainable code which is way more simpler then writing Raw SQL queries or using Query Builder. For Example:-

Use Eager Loading

Most of the time developer write such DB queries which leads to the N+1 problem which means for 100 users 101 DB queries will be executed which is a bad practice. Instead, Laravel provides Eager loading using which you can completely avoid N+1 problem. For example:-

Now for 100 users only 2 DB queries will be executed which is way more efficient than the other solution.

Read More :- Laravel Upgrading To 5.8.0 From 5.7

Fat Models And Skinny Controllers

One of the good practice consists of wrting all DB related logic into model and having controller as light as possible. This will make handling code more easy and maintainable.For Example:

Use shorter and more readable syntax where possible

You should always use shorter and more readable syntax while writing your code. This will make the code more managable and easy to read.For example:

Do Not Get Values from.env File Directly

You should not get any values from the .env files instead you should be using config files to get the values that you may need. For example:

There are other things also that you should consider while writing a Laravel code. Naming convention, using standard tools approved by the Laravel community, Never put logic into route files, etc. I hope this would have some things clear for you and If you like this article, then gives us a thumbs up and will come up the Part-2 of the Laravel Best Practices In 2019.

All Rights Reserved @ Techanical-Atom
%d bloggers like this: