Laravel Naming Conventions for Better Understanding the Code

As a developer we need a best namig convertions for our coding side because the code is best for everyone easily understand. Thousands of developers are working now a days in IT sector but unfotunatily little are knows the namig conventions for working with code. We must use the name convention for our work for better understading to other who come with work as a team so he can easily understand the code and work smartly.

Here we are going to share how to use controllers, model, migration (database table name), blade files (view), class names, div ids, variables name, funcation name with namig conventions etc. So let’s start here with this best tutorial of Laravel Naming Conventions for fresher or experience level artisans.

Naming Conversion with Controllers

Its time to naming conversion for controller name for what is the controller name is suitable for writing best namig.

Controllers should be in singular case no spacing between words we known this type namig is PascalCase and not plurel just like below.

PostController, UserController, ProfileController

Naming Conversion with database tables

As we all knows the below command generate model and migration files already and you must use just singuler model name here and the command automatically generate plurale or whats you want database table name.

WIth Specific Table:

Here I am create Post model which will generate posts table after running the migration command. The Model created with PascalCase and the migration with snake_case.

First run this following command

php artisan make:model Post -m

This command generate Post model with posts table, if you run php artisan migrate command then you see a posts table is automatically generted in your database.

With Pivot tables (Relational Table):

Pivot tables automatically generated lower case if we use the following command for creating relational model, migration.

php artisan make:model PostComment -m

This command generate a PostComment Model with post_comments table.

Relational Table Columns Names:

Now you want to know what is the columns in Pivot table (relation) or primary table. the column of any database table will be singuler and the namig convantion is snake_case.

post_id, user_id etc

Here in post_comments table we add post_id … etc.

Namig Conversion for Blade Files

Blade file should be kebab-case in lower underscore between words.

post.blade.php, post-comments.blade.php

You can use undrescore for creating blade files but we give most priority for with underscore.

Naming Conventions for models

As above we mentioned for model and migration files name conversion we show you briefly it.

For example

You can create mode insie app or create a new folder Models and then create inside that.

In laravel 8 the model name created automatically in models folder and the folder automatically create to generate php artisan make:model command. Here you can use just like below for your model

For example: Post (\App\Post or \App\Models\Post, etc)

Model Methods

Its defenitly needs if we use methods inside model … We can use hasOne, hasMany, belongsTo, belongsToMany or any else then how we use them in model just see below.

Relationship with hasOne and hasMany

For HasOne Method: If the relation return single record then the methods name should be camelCase with singuler.

public function postComment()
{
    return $this->hasOne(PostComment::class);
}

For HasMany Method: And If the relation return multiple record then the methods name should be camelCase with plural.

public function postComments()
{
    return $this->hasMany(PostComments::class);
}

Relationship with belongsTo and belongsToMany

This is same as hasOne and hasMany methods for post model just for example you can use with your models.

For BelongsTo Method: Same as hasone the belongs to with camel case (Singuler).

public function comment()
{
    return $this->belongsTo(Comment::class);
}

For BelongsToMany Method: And the belngsToMany name should be camelCase (plural)

public function comments()
{
    return $this->belongsToMany(Comment::class);
}

Naming Conversion for Funcation name

Funcation name should be in camelCase if you using funcation (methods) in controller or model etc.

If you are using resources routes then you can use your generate methods create, edit, delete, store, update routes etc..

public function store(Request $request)
{

}

If you are creating new funcation excepts the resources methods you can use camelCase for funcation name.

public function storeFiles(Request $request)
{
  
}

Update the funcation in model same as

If your model is PostComment and you are using in controller then use just lile below

public function postComment()
{
   return $this->belongsTo(PostComment::class);
}

Naming Conversion for Class Name

Class name should be in kebab-case, its better to use the div classes in laravel or any other framework or cms.

user-sections, post-section, card, post-card-section, post-inner-section etc.

Naming Conversion for Id

Many developer don’t know which type of namic best for id class etc. We will describe the ideas for adding id for any bootstrap model, form or any input, label or div etc.

The best id is with underscore (kebab-case) or camelCase just like below.

post-table, postTable, postForm, post-form, postModel, post-model etc.

 Laravel Naming Conventions in Table for Better understanding

WhatHowGoodBad
ControllersingularPostControllerPostsController
Routepluralposts/1post/1
Named routesnake_case with dot notationposts.show_activeposts.show-active, show-active-posts
ModelsingularPostPosts
hasOne or belongsTo relationshipsingularpostCommentpostsComments, post_comment
All other relationshipspluralpostCommentspostComment, post_comments
Database Tablepluralpost_commentspost_comment, postComments
Pivot tablesingular model names in alphabetical orderpost_commentcomment_post, posts_comments
Table columnsnake_case without model namemeta_titleMetaTitle; post_meta_title
Model propertysnake_case$model->created_at$model->createdAt
Foreign keysingular model name with _id suffixpost_idPostId, id_post, posts_id
Primary keyidcustom_id
Migration2017_01_01_000000_create_posts_table2017_01_01_000000_posts
MethodcamelCasegetAllget_all
Method in resource controllertablestoresaveArticle
Method in test classcamelCasetestGuestCannotSeePosttest_guest_cannot_see_post
VariablecamelCase$postsWithAuthor$posts_with_author
Collectiondescriptive, plural$activePosts = Post::active()->get()$active, $data
Objectdescriptive, singular$activePost = Post::active()->first()$posts, $obj
Config and language files indexsnake_caseposts_enabledPostsEnabled; posts-enabled
Viewwith hyphenshow-filtered.blade.phpshowFiltered.blade.php, show_filtered.blade.php
Configsnake_casegoogle_calendar.phpgoogleCalendar.php, google-calendar.php
Contract (interface)adjective or nounAuthenticatableAuthenticationInterface, IAuthentication
TraitadjectiveNotifiableNotificationTrait

So, friends today we learn about Laravel Naming Conventions for Better Unterstanting. which type namig conventions you are working describe us and why its most important, we will update your comments on code. Share this post for other artisans and follow us for more updates.

Leave a Comment