Laravel Framework Naming Convention

In order to leverage on the Laravel Framework, its essential to apply the naming conventions. 

Here are the basics on my recommended naming convention based on current Laravel practices and recommendations at PSR 4 and other PSR

Database

Tables names

  1. Lower case with underscore to separate words
  2. In Plural form
  3. No spaces
Example:
orders, customers, customer_locations

Column names

  1. Lower case with underscore to separate words
  2. Singular form
  3. Shouldn't reference table name. For example, column name id is unique and used to keep track of specific row. 

Pivot tables

  1. Lower case with underscore to separate words
  2. In singular form
Example:
customer_order

Foreign keys

  1. Lower case with referenced model name and append with '_id'
  2. In singular form
Example:
customer_id, order_id

Variables

  1. start with lower case is camelCase.
  2. In singular form
  3. When variables contain array or collection of items, then name is in plural form
Example:
$customers = Customer::all( );
$customer = Customer::first( );

Controllers

  1. Begin with Upper case, is camelCase and appends with "Controller"
  2. In Singular form
Example:
CustomerController

Models

Model names

  1. Begin with upper case, is camelCase to separate words.
  2. In Singular form
  3. Avoid short form of words
Example:
Customer, User

Model properties

  1. Lower case with underscore to separate words
  2. Is singular, and follow the related table column name
Example:
$updated_at, $description

Model methods

  1. Begin with lower case and is camelCase
  2. Singular or Plural depends on the return value. 
  3. Avoid repeating the class name where possible.
Example:
index( ), reset( ), getOrder( ), getOrders( )

Routes

  1. All in lowercase and use the hypen to separate words.
  2. Use curly braces to store object(s)
  3. Use correct HTTP methods (GET, POST, PUT, DELETE) to determine which controller method is called
  4. Use standard route name (index, create, store, show, edit, update, destroy)

Blade view

View Folders

  1. Begin with lower case and is camelCase
  2. In Singular form
  3. No spaces
Example:
customer, order

View Files

  1. Lower case with underscore to separate words
  2. Descriptive and avoid abbreviations or short names
  3. No spaces
Example:
show.blade.php, index.blade.php


No comments:

Blog Archive