[Feature Request] Add some option to switch to singular table names by default #42015
Replies: 4 comments 4 replies
-
I agree this should be revisited. Times have changed, you could say people have "seen the light". This StackOverflow answer, if I'm remembering correctly from years ago, used to be a fairly even 50/50 vote split between plural and singular table names: https://stackoverflow.com/questions/338156/table-naming-dilemma-singular-vs-plural-names Today, if you sort by vote, the top answer is "Singular table names", with a score of 2342, and the top "plural" answer is a measly 145. Based on these votes, over 94% of people prefer singular names to plural, so Laravel alienates over 94% of developers by sticking with this old standard. Honestly, the next version of Laravel should probably have singular names by default, with an option (in the Yes, we could make this argument or that argument, saying which naming scheme is better. But there's no denying that it appears that a larger number of people seem to prefer singular database names. |
Beta Was this translation helpful? Give feedback.
-
Why "artisans" hate writing:
We also had a request of auto-generating the resource name based on the model name just so the resource name could NOT be defined in each model. As a conclusion this issue that you rose is a non issue if the table would had not been guessed by laravel based on the model name. Also, REST requires resource name to be in plural form. So, it makes sense for classes like service and controller to have the plural form name and classes like model and decorator for that model to have the singular form name. On the same analogy the table name should be plural because it holds MANY not just ONE row. |
Beta Was this translation helpful? Give feedback.
-
Question here is: Is the distinction between singular and plural sufficient or will there be people next who want a prefix ( |
Beta Was this translation helpful? Give feedback.
-
Solution for customizing the table name overwrite this function in your base model /**
* Get the table associated with the model.
*
* @return string
*/
public function getTable()
{
return $this->table ?? Str::snake(Str::pluralStudly(class_basename($this))); // replace this with what best suits you
} |
Beta Was this translation helpful? Give feedback.
-
One thing I disagree with is the default naming of table names using plural forms. I read the reasoning behind it in the documentation and although I get it, it doesn't seem a strong enough reason to go against most MySQL best practices and conventions of using singular form for table names.
I know that changing this now to singular would be a nightmare from a backwards compatibility standpoint and a lot of stuff would have to be changed, but that doesn't mean it can't be at least optional. I know you can set the table name individually for each model with this:
But doing that for all the models is unnecessary work especially when the name only differs with the missing
s
at the end (in most cases).Another workaround that I'm personally using is to create my own BaseModel class that overrides the
getTable
method and removes the pluralization of the name and have all other models extend that class instead of the default Model. However even this seems like too much for removing just one function from a single method. Also I'm not sure if this is the only place that needs changing and can't be sure if something doesn't break down the line.So my proposal is to add some option flag that you'll set in your config or
.env
file and Laravel will automatically use plural or singular table names based on the settings. To maintain BC and make the change have little impact, the plural can still remain the default option if the flag isn't set anywhere and only switch to singular after setting it.So the flag could be like this in the
.env
file:And in the Model class the
getTable
would look something like this (plus change it like this in any other places that might need changing):Please do consider this as it would be so greatly appreciated to have this option, so that you won't have to write your own solutions or extra code if you just want to follow MySQL conventions and use singular form for table names.
Beta Was this translation helpful? Give feedback.
All reactions