Laravel如何找到模型的复数形式? [英] How does Laravel find plural of models?
问题描述
如果我有模型"Dog",Laravel会将其链接到表"Dogs".总是复数. 现在,如果我有一个模型"Person",它将尝试查找表"People"-也是复数.但是,Laravel如何知道不仅仅是加一个"s"的复数呢? 是否有一个包含所有英语名词的表格?
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 plural. But how does Laravel know the plural when it's more than just adding a "s"? Is there a tabel with all english nouns?
推荐答案
Laravel 4
在Illuminate\Database\Eloquent\Model.php
中,您会找到类似str_plural($name)
的内容,并且str_plural
是使用Str::plural
方法的辅助函数,在这种情况下,该方法如下所示:
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);
}
因此,很明显,Str::plural
使用类Illuminate\Support\Pluralizer.php
,在那里您将发现它的实际工作方式.只需阅读源代码即可. irregular word forms
与其他人有一个单独的单词映射:
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',
);
这篇关于Laravel如何找到模型的复数形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!