Laravel coding convention

Category : Laravel Coding, Programming Tags : Laravel, Laravel Development, Laravel Tutorial

Naming Controllers

Controllers should be in singular case, no spacing between words, and end with "Controller".

Also, each word should be capitalized (i.e. BlogController, not blogcontroller).

Correct Example: BlogController, AuthController, UserController.

Wrong Example: UsersController (because it is in plural), Users (because it is missing the Controller suffix).

Naming database tables in Laravel

DB tables should be in lower case, with underscores to separate words (snake_case), and should be in plural form.

Correct Example: posts, project_tasks, uploaded_images.

Wrong Example: all_posts, Posts, post, blogPosts

Pivot tables (Third table in many to many relationship)

Pivot tables should be all lower case, each model in alphabetical order, separated by an underscore (snake_case).

Correct Example: post_user, task_user etc.

Wrong Example: users_posts, UsersPosts.

Table columns names

Table column names should be in lower case, and snake_case (underscores between words). You shouldn't reference the table name.

Correct Example: post_body, id, created_at.

Wrong Example: blog_post_created_at, forum_thread_title, threadTitle.

Primary Key

This should normally be id.

Foreign Keys

Foreign keys should be the model name (singular), with '_id' appended to it (assuming the PK in the other table is 'id').

Correct Example: comment_id, user_id.

Variables

Normal variables should typically be in camelCase, with the first character lower case.

Correct Example: $users = ..., $bannedUsers = ....

Wrong Example: $all_banned_users = ..., $Users=....

If the variable contains an array or collection of multiple items then the variable name should be in plural. Otherwise, it should be in singular form.

Correct Example: $users = User::all(); (as this will be a collection of multiple User objects), but $user = User::first() (as this is just one object)

Naming Conventions for models

Naming Models in Laravel

A model should be in singular, no spacing between words, and capitalised.

Correct Example: User (\App\User or \App\Models\User, etc), ForumThread, Comment.

Wrong Example: Users, ForumPosts, blogpost, blog_post, Blog_posts.

Model properties

These should be lower case, snake_case. They should also follow the same conventions as the table column names.

Correct Example: $this->updated_at, $this->title.

Wrong Example: $this->UpdatedAt, $this->blogTitle.

Model Methods

Methods in your models in Laravel projects, like all methods in your Laravel projects, should be camelCase but the first character lower case.

Correct Example: public function get(), public function getAll().

Wrong Example: public function GetPosts(), public function get_posts().

Relationships

hasOne or belongsTo relationship (one to many) These should be singular form and follow the same naming conventions of normal model methods (camelCase, but with the first letter lower case) For example: public function postAuthor(), public function phone(). hasMany, belongsToMany, hasManyThrough (one to many) These should be the same as the one to many naming conventions, however, it should be in plural.

Correct Example: public function comments(), public function roles().

Polymorphic relationships

These can be a bit awkward to get the naming correct.

Ideally, you want to be able to have a method such as this:

public function category()

{

return $this->morphMany('App\Category', 'categoryable');

}

And Laravel will by default assume that there is a categoryable_id and categoryable_type.

But you can use the other optional parameters for morphMany ( public function morphMany($related, $name, $type = null, $id = null, $localKey = null)) to change the defaults.

Method naming conventions in controllers

These should follow the same rules as model methods. I.e. camelCase (first character lowercase).

In addition, for normal CRUD operations, they should use one of the following method names.

Verb URI Typical Method Name Route Name



GET photos index() photos.index GET photos create create() photos.create POST photos store() photos.store GET photos {photo}show() photos.show GET photos {photo} edit edit()photos.edit PUT/PATCH photos {photo} update() photos.update DELETE photos {photo} destroy() photos.destroy

Traits

Traits should be be adjective words.

Correct Example: Notifiable, Dispatchable, etc.

Blade view files

Blade files should be in lower case, snake_case (underscore between words).

Correct Example: all.blade.php, all_posts.blade.php, etc