Laravel 假设数据库表是模型名称的复数形式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37674309/
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 13:56:45  来源:igfitidea点击:

Laravel assumes the database table is the plural form of the model name

laraveleloquent

提问by Mervyn Yet

By default, Laravel is assuming that the database table is the plural form of the model name. But what if my table name is "news" and i still want to use this feature? should i change it to "newses" or should i use "new" for the model name?

默认情况下,Laravel 假设数据库表是模型名称的复数形式。但是如果我的表名是“news”并且我仍然想使用这个功能怎么办?我应该将其更改为“newses”还是应该使用“new”作为模型名称?

回答by Koen B.

If you have a model ending with the letter 's', it will keep the table name the same. In your case, your model will name your table newsby default.

如果您有一个以字母“s”结尾的模型,它将使表名保持不变。在您的情况下,您的模型将news默认为您的表命名。

If you want to use another name though, you can use:

如果你想使用另一个名字,你可以使用:

protected $table = 'tablename';

inside of your model.

在你的模型里面。

EDIT: I tested this in my application. I made a model named News. Then I made a new instance of Newsand retrieved the table name:

编辑:我在我的应用程序中测试了这个。我做了一个名为News. 然后我创建了一个新实例News并检索了表名:

$news = new News();
dd($news->getTable());

It returns: news

它返回: news

回答by Vijayanand Premnath

You may specify a custom table by defining a table property on your model as below

您可以通过在模型上定义表属性来指定自定义表,如下所示

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
/**
 * The table associated with the model.
 *
 * @var string
 */
protected $table = 'my_flights';
}

Ref:https://laravel.com/docs/5.1/eloquent

参考:https: //laravel.com/docs/5.1/eloquent

回答by KuKeC

Inside your eloquent model you have to define table name. For example if my model is named userand table in database is named user_of_applicationthen i do it this way

在您雄辩的模型中,您必须定义table name. 例如,如果我的模型被命名user并且数据库中的表被命名,user_of_application那么我这样做

class user extends Model
{
    protected $table = 'user_of_application';
}

回答by Justin Origin Broadband

Laravel uses a "standard set" rule that defines:

Laravel 使用“标准集”规则定义:

  • A Model is a single instance of a record
  • A Collection is one or more records
  • 模型是记录的单个实例
  • 一个集合是一个或多个记录

Therefore it assumes that a table is a collection of records.

因此,它假定表是记录的集合。

The nomenclature has a problem when it clashes with various features / gotchas of human languages. For example, what if I have a Model called Sheep? That does mean my Database Table should be called "Sheeps"?

当命名法与人类语言的各种特征/陷阱发生冲突时,它会出现问题。例如,如果我有一个名为 Sheep 的模型怎么办?这是否意味着我的数据库表应该被称为“羊”?

It's up to the developer to avoid "Gollum/Smeagol" syntax. Indeed, you wouldn't want a table called "Newses" as much I'd like to end up with a table called "Sheeps".

开发人员应该避免使用“Gollum/Smeagol”语法。确实,您不会想要一张名为“Newses”的表,而我希望最终得到一张名为“Sheeps”的表。

Ultimately, I construct Migrations with:

最终,我使用以下方法构建迁移:

sudo php artisan make:migration create_sheep_table --create=sheep

As for Models, you'll notice in the documentation that they have a different table name for "Flights" called "my_flights"

至于模型,您会在文档中注意到它们有一个不同的“航班”表名称,称为“my_flights”

https://laravel.com/docs/master/eloquent#defining-models

https://laravel.com/docs/master/eloquent#defining-models

Again, it's up to the developer / DB manager to make decisions on naming conventions that make sense in an application context.

同样,由开发人员/数据库管理员决定在应用程序上下文中有意义的命名约定。