Laravel 如何找到多个模型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23943663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:34:35  来源:igfitidea点击:

How does Laravel find plural of models?

laravellaravel-4

提问by almo

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?

如果我有一个模型“Dog”,Laravel 会将它链接到表“Dogs”。总是复数。现在,如果我有一个模型“人”,它会尝试找到表“人”——也是复数。但是当 Laravel 不仅仅是添加一个“s”时,它如何知道复数呢?有没有包含所有英语名词的表格?

回答by The Alpha

Laravel 4

Laravel 4

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

Illuminate\Database\Eloquent\Model.php你会发现类似str_plural($name)str_plural是一个使用Str::plural方法的辅助函数,在这种情况下,这个方法看起来像这样:

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

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

所以很明显,Str::plural使用 classIlluminate\Support\Pluralizer.php并且在那里你会发现它是如何实际工作的。只需阅读源代码。irregular word forms与其他人有一个单独的词映射:

// 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',
);

回答by andrewtweber

Laravel 5 & 6

Laravel 5 & 6

The Alpha's answer was for Laravel 4.

Alpha 的答案是针对 Laravel 4。

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

为了给予应有的信任,我想更新 Laravel 5+ 的答案。

Pluralizernow extends from doctrine/inflectorto avoid re-inventing the wheel. This library contains some basic rules, e.g.

Pluralizer现在从教义/影响因素延伸以避免重新发明轮子。这个库包含一些基本规则,例如

(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.

Doctrine inflector 具有用于使文本变形的静态方法。

这些类中的方法来自从几个不同的 php 项目和几个不同的作者收集的几个不同的来源。原作者姓名和电子邮件未知。

Pluralize 和 Singularize 实现是从 CakePHP 借来的,并进行了一些修改。

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

因此,有趣的是所有框架之间有多少相互借用和重用。