Let's talk about singular vs plural in Eloquent Model.
Typically, the Model name should be singular in the make:model
artisan command, like make:model Post
instead of Posts
. This means Laravel will try to figure out what database table to work with. The table name would be pluralized from the Model name.
If you create a Model Project
with the Migration, Laravel will name the table projects
.
This is a typical behavior and best practice but not a rule. If you generate Projects
instead of a Project
, it would work. But I don't advise and recommend such an approach. However, it is not a strict rule.
Also, let's look at the singular and plural in multiple words. For example, you have a projects
table and you want to create a project type
Model. But should it be ProjectType
or ProjectsType
? Let's generate a Model with Migration and Controller and see the names.
We have a ProjectType
Model, a project_types
table, and a ProjectTypeController
. So, it doesn't change anything in the Controller name but adds a pluralized form in the table name.
If you have multiple words in the Camel Case, then in the snake_case form, the second word becomes plural. Again, you can overwrite it and use, for example, projects_types
for the table name.
I'm not too fond of this approach and try to stick with the so-called Laravel naming and best practices.
But, finally, if your table name is entirely different from the Model name, you can always override the conventions and provide the table name in the Model.
class Role extends Model{ protected $table = 'user_roles';}
No comments or questions yet...