模型和表命名约定 Laravel

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

Model and table name conventions Laravel

laravelmodels

提问by Harman

I want to know model and table conventions in Laravel.

我想知道 Laravel 中的模型和表格约定。

I have table users and user_address now I do not want to use

我现在有表 users 和 user_address 我不想使用

protected $table = "user_address"

受保护的 $table = "user_address"

property. Please suggest me the right convention in model or table.

财产。请建议我在模型或表格中使用正确的约定。

回答by Alexey Mezenin

If the name of the model is UserAddress, the name of the table should be plural. So it will be user_addresses.

如果模型UserAddress名称为 ,则表名称应为复数形式。所以会的user_addresses

Read more about Laravel naming conventions here.

在此处阅读有关 Laravel 命名约定的更多信息。

回答by Sapnesh Naik

If your model name is User.php then Laravel expects class 'User' to be inside that file. It also expects userstable for User model (plural form).

如果您的模型名称是 User.php,那么 Laravel 期望类 'User' 位于该文件中。它还需要用户模型的用户表(复数形式)。

Similiarly, If you have a user_addressmodel your table should be user_addresses

同样,如果你有一个user_address模型,你的表应该是user_addresses