How to do Best Coding Practices in Laravel

The topics that will be discussed in this Article are as follows:

  • Basic and advanced practices
  • Real-life examples of design patterns used in Laravel
  • The reasons why these design patterns are used in the examples

Basic practices

As a developer, when you are working on an application, there should be a systematic order to things to prevent confusion and allow flexibility. For example, in an MVC architecture, Controller should only hold the logic and Model should only hold dataflow-related stuff. You should not write database queries in View files. This way, anyone working on the project can find what they are looking for easily and can change, fork, or improve it with greater ease. If this is not followed, the project will turn into a mess as it gets bigger and bigger.

Recommended :  Descending the code in laravel 5.1

A basic good practice would be to avoid repeating yourself. If you’re using a code snippet or a condition a number of times, it’d be better for you to prepare a method or a scope for that action. This way, you wouldn’t have to repeat yourself over and over. For example, let’s say we have an imaginary Controller as follows:

 

<?php

class UserController extends BaseController {

   //An imaginary method that lists all active users
   public function listUsers() {

      $users = User::where('active', 1)->get();

      return View::make('frontend.users.list')
         ->with('users', $users);
   }


   //An imaginary method that finds a specific user
   public function fetch($id) {

      $user = User::where('active', 1)->find($id);

       return View::make('frontend.users.single')
         ->with('user', $user);

   }

}

As you can see, the where() condition checks if active is repeated twice. In real-world examples, it would be used even more.

Recommended :  Generate Laravel Migrations from an existing database

To prevent this, in Laravel, you can use query scopes. Query scopes are single functions that help you reuse the logic in Models. Let’s define a query scope in Model and change the Controller method as follows:

<?php

//Model File
Class User extends Eloquent {

   //We've defined a Query scope called active
   public function scopeActive($query) {
      return $query->where('active', 1);
   }

}




//Controller File
class UserController extends BaseController {

   //An imaginary method that lists all active users
   public function listUsers() {

      $users = User::active()->get();

      return View::make('frontend.users.list')
         ->with('users', $users);
   }


   //An imaginary method that finds a specific user
   public function fetch($id) {

      $user = User::active()->find($id);

      return View::make('frontend.users.single')
         ->with('user', $user);

   }

}

As you can see, we’ve defined a method called scopeActive() in Model, which is prefixed with the word scope and CamelCased. This way, Laravel can understand that it’s a query scope, and you can use that scope directly. As you can see, the conditions in the Controller have also changed. They have changed from where('active', 1) to active().

Design patterns are advanced practices and can be used to keep the code tidy and systematic using various approaches.

Recommended :  Create Movies and Actors Databases with laravel

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

Pin It on Pinterest

Shares