How does Laravel find plural of models?

前端 未结 2 1053
野性不改
野性不改 2020-12-09 16:06

If I have a Model \"Dog\", Laravel will link it to the table \"Dogs\". Always the plural. Now, if I have a Model \"Person\", it tries to find the table \"People\" - also the

2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-09 16:20

    Laravel 4

    In the Illuminate\Database\Eloquent\Model.php you'll find something like str_plural($name) and str_plural is a helper function which uses Str::plural method and in this case, this method looks like this:

    public static function plural($value, $count = 2)
    {
        return Pluralizer::plural($value, $count);
    }
    

    So it's obvious that, Str::plural uses class Illuminate\Support\Pluralizer.php and there you'll find how it actually works. Just read the source code. There is a separate word mapping for irregular word forms with others:

    // Taken from Illuminate\Support\Pluralizer
    public static $irregular = array(
        'child' => 'children',
        'foot' => 'feet',
        'freshman' => 'freshmen',
        'goose' => 'geese',
        'human' => 'humans',
        'man' => 'men',
        'move' => 'moves',
        'person' => 'people',
        'sex' => 'sexes',
        'tooth' => 'teeth',
    );
    

提交回复
热议问题