Laravel 5 Naming Conventions
Tables: posts, comments, comment_post
Columns: id, post_id, comment_id
Controllers: PhotoController, TaskController
Models: Page, Card, Post
For more details check out my Laravel naming conventions table.
Laravel naming convention for blade files
I came across Laravel Best Practices.
Laravel : Best Practices aims to put together all the resources and best practices in using the Laravel Framework. Last Updated: 2020-05-07 12:26:48
Views
You SHOULD use snake_case as file name of your Blade templates
Good
show_filtered.blade.php
Bad
showFiltered.blade.php
show-filtered.blade.php
Laravel Models and Controller files naming
What you're describing sounds less like a naming convention issue and more of a PSR-4 compliance issue.
Specifically, point 2.6:
All class names MUST be referenced in a case-sensitive fashion.
So for example, since you named the file post.php
then the file must contain a class post
and be referenced as new post()
. This will ensure the composer PSR-4 autoloader will pick it up.
If you, however, have a file called Post.php
then it must contain a class Post
and be referenced as new Post()
.
Laravel's naming convention
Illuminate/Pluralizer is responsible to get the inflection of an English word. It defines a set of uncountable words like:
public static $uncountable = [
'audio',
'bison',
...
'species',
'swine',
'traffic',
'wheat',
];
The Pluralizer
internally uses doctrine/inflector
which defines the actual logic (collected from several different php projects by several different authors) to inflect a word. Inflector
has general rules to plularize and singularize words, and also defines words which are uninflected, or have irregular plurals.
private static $plural = array(
'rules' => array(
...
'/(x|ch|ss|sh)$/i' => '\1es',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/us$/i' => 'uses',
...
),
'uninflected' => array(
'.*[nrlm]ese', '.*deer', '.*fish', ..., 'people', 'cookie'
),
'irregular' => array(
'atlas' => 'atlases',
'axe' => 'axes',
'beef' => 'beefs',
...
'trilby' => 'trilbys',
'turf' => 'turfs',
'volcano' => 'volcanoes',
)
)
Laravel 5: naming convention for file names
According to the PSR-4 convention:
http://www.php-fig.org/psr/psr-4/
So that defines your name spaces. The convention is to match your directory names to your namespaces. If this were java, they'd all be lower case. However, in PHP, the namespace convention is to keep them all upper case. The other convention is that your namespace should match your directory structure for non third party code. However do keep in mind that with psr-4 this is not required.
laravel controller name should be plural or singular?
Here is a list of naming conventions accepted by Laravel community. According to this, Controller name should be Singular although you could chose your own convention as your need or how your team prefer.
Laravel naming convention with similar Model names
If you use polymorphic relations you can even make a Comments has its own comments (like a reply system).
In the model you want to have comments you need to add a method like this:
public function comments() {
return $this->morphMany(Comment::class, 'commentable');
}
Then in your Comment model, you just add
public function commentable() {
return $this->morphTo();
}
Notice how important it is that your method name in Comment matches with the second parameter passed to the morpMany() function.
Last but not least, the model in which you morph to needs to have special fields in its schema:
You must place them at the end of the traditional schema structure:
// Your comment dedicated schema
$table->integer('commentable_id');
$table->string('commentable_type');
I really don't know if it is important but notice how the field prefix matches with the method name in which we are morphing.
How to avoid mixed naming conventions on Laravel Eloquent Model properties and methods
SQL is case insensitive so
select capacityQty from CapacityUnit
//is equivalent to
select capacityqty from capacityunit
Hence it has been a convention to use lowercase for table and column names.
In Laravel model properties represent column names for the table wherein all records for the Model's entity will be stored
Hence following the conventions of SQL, Laravel also uses lower case for table names and Model's property names
However for class names, it follows the convention of using PascalCase - widely accepted and followed in various Object Oriented programming languages
I totally understand both conventions and both make sense. Howeve it leads to an inconsistent way of accessing properties in models with relations.
Unfortunately/fortunately yes - that's due to the fact - that SQL/databases and Object Oriented programming languages are basically different worlds and follow different conventions.
You can follow a consistent convention in naming your tables and column names. For eg:
Table name: PascalCase or lowercase plural as per convention
Column name: camelCase
Being consistent is the key thing to remember.
However following the above convention will require some extra work when working with Laravel
- Declare the table property on model class
protected $table = 'CapacityUnit'
- Specify the column name when defining relationships, if the foreign key is in camelCase and table name
Then you can use camelCase for model property names.
But as is evident - trying to do consistent way of accessing properties using camelCase necessitates significant extra efforts when using Eloquent and seems not worth. Plus there's always a possibility of human error factor especially in team environment.
For most part using relational databases like MySQL, Postgres , SQL Server, there would not be any major issue except may be some side effects in certain edge cases.
However there are some modern databases which follow a convention of either all uppercase or lowercase, where there could be significant side effects.
Related Topics
Php: Get List of All Filenames Contained Within My Images Directory
Url Encode Equivalent in Ruby on Rails
Using Ajax in a Wordpress Plugin
Xml Creation Using Codeigniter
Remotely Connecting to a MySQL Database
Running Imagemagick on Xampp 1.8.3 with Windows 7
Assets Not Referencing to Public Folder (Laravel)
Check If Two Arrays Have the Same Values
How to Access a Different Controller from Inside a Controller Symfony2
Symfony2: How to Access Service from Template
How to Convert All Characters to Their HTML Entity Equivalent Using PHP
Warning: "Continue" Targeting Switch Is Equivalent to "Break". Did You Mean to Use "Continue 2"