Laravel 如何找到复数模型? [英] How does Laravel find plural of models?

查看:16
本文介绍了Laravel 如何找到复数模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个模型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

IlluminateDatabaseEloquentModel.php 你会发现像 str_plural($name)str_plural 是一个使用 Str::plural 方法的辅助函数,在这种情况下,此方法如下所示:

Laravel 4

In the IlluminateDatabaseEloquentModel.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 使用类 IlluminateSupportPluralizer.php,你会发现它实际上是如何工作的.只需阅读源代码.不规则词形与其他有单独的词映射:

So it's obvious that, Str::plural uses class IlluminateSupportPluralizer.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 IlluminateSupportPluralizer
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆