Laravel

Models

Ordering

Our preferred ordering of items within a Laravel Model class is as follows:

  • Traits
  • Constants
  • Laravel specific fields ($table, $guarded, $fillable etc)
  • Public fields
  • Private fields
  • Relations
  • Scopes
  • Other Methods

Routing

The following guidelines are applicable to Laravel's Routingopen in new window.

Method definitions

Laravel provides a few differing ways to define routing within your application. When defining routes, explicit is the way to go - using Laravel magic 🧙‍♂️ like resources can make code difficult to understand.

❌  Incorrect

Route::post(...);
Route::get(...);
Route::delete(...);
Route::resource(...);
$router->resource(...);

✅  Correct

use Illuminate\Routing\Router;

$router = new Router();

$router->patch(...);
$router->post(...);
$router->get(...);
$router->delete(...);

Parameter definitions

Routes require some parameters to be defined, such as the controller method, they also allow additioanl parameters such as a name to be defined. In true Laravel fashion, there are multiple ways to do this.

❌  Incorrect

$router->get('profile', 'ProfileController@index')->name('profile');

✅  Correct

$router->get('profile', [
    'uses' => 'ProfileController@index',
    'as' => 'profile',
]);

Validation

Validation for request should be implemented by adding the validation rules to the rules() method on Request classes where possible - these help encapsulate validation and authorisation for the request. We discourage using Validator::make() unless it is absolutely necessary and Request classes can not be utilised.