How does Laravel find plural of models?

前端 未结 2 1030
野性不改
野性不改 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:18

    Laravel 5 & 6

    The Alpha's answer was for Laravel 4.

    To give credit where it is due I wanted to update the answer for Laravel 5+.

    Pluralizer now extends from doctrine/inflector to avoid re-inventing the wheel. This library contains some basic rules, e.g.

    (m|l)ouse         => _ice
    (buffal|tomat)o   => _oes
    ...all else...    => append 's'
    

    Followed by some "uninflected" (i.e. singular and plural are the same)

    deer, fish, etc.
    

    And finally the irregular rules, e.g.

    man  => men
    ox   => oxen
    

    From the documentation:

    Doctrine inflector has static methods for inflecting text.

    The methods in these classes are from several different sources collected across several different php projects and several different authors. The original author names and emails are not known.

    Pluralize & Singularize implementation are borrowed from CakePHP with some modifications.

    So it's interesting how much all of the frameworks borrow and reuse from one another.

提交回复
热议问题