Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel's naming convention [duplicate]

Tags:

laravel

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?

like image 522
Steve Avatar asked Oct 19 '25 12:10

Steve


2 Answers

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',
    )
)
like image 109
John Bupit Avatar answered Oct 21 '25 08:10

John Bupit


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.

like image 33
Alexey Mezenin Avatar answered Oct 21 '25 06:10

Alexey Mezenin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!