If the Model is Company
it's table name is companies
if we do not override protected $table
property. And if it is Book
, table name is books
. So why not companys
How does laravel understand English and grammer? Where are these words listed?
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 is smart enough to use real words. For example, it will use media
table for Media
model.
If you'll see in the source code, you'll see how does Laravel builds table name:
return str_replace('\\', '', Str::snake(Str::plural(class_basename($this))));
First, Laravel uses Pluralizer::plural()
to create plural and then converts it to snake case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With