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
What | How | Good | Bad |
---|---|---|---|
Controller | singular | PostController | |
Route | plural | posts/1 | |
Named route | snake_case with dot notation | posts.show_active | |
Model | singular | Post | |
hasOne or belongsTo relationship | singular | postComment | |
All other relationships | plural | postComments | |
Database Table | plural | post_comments | |
Pivot table | singular model names in alphabetical order | post_comment | |
Table column | snake_case without model name | meta_title | |
Model property | snake_case | $model->created_at | |
Foreign key | singular model name with _id suffix | post_id | |
Primary key | – | id | |
Migration | – | 2017_01_01_000000_create_posts_table | |
Method | camelCase | getAll | |
Method in resource controller | table | store | |
Method in test class | camelCase | testGuestCannotSeePost | |
Variable | camelCase | $postsWithAuthor | |
Collection | descriptive, plural | $activePosts = Post::active()->get() | |
Object | descriptive, singular | $activePost = Post::active()->first() | |
Config and language files index | snake_case | posts_enabled | |
View | with hyphen | show-filtered.blade.php | |
Config | snake_case | google_calendar.php | |
Contract (interface) | adjective or noun | Authenticatable | |
Trait | adjective | Notifiable |
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.