[Feature Request] Add some option to switch to singular table names by default #42015
Unanswered
matronator
asked this question in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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