laravel 约定表名(带下划线)

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

Convention table names (with underscore)

phpormlaraveleloquent

提问by Patrick Maciel

What is the correctly table nameof this table in Laravel 3/4?

Laravel 3/4 中这张表的正确表名是什么?

Structure
image_projects (id, project_id, image, ext, size, created_at, updated_at, active)

结构
image_projects (id, project_id, image, ext, size, created_at, updated_at, active)

image_projects
imageprojects
imageProjects

And, how I can create the Model?

而且,我如何创建模型?

app/models/image_projects.php
app/models/imageprojects.php
app/models/imageProjects.php
app/models/image/projects.php
app/models/projects/image.php

回答by Joseph Silber

It makes no difference what you name your table, as long as you then name the file & class in singular form, with the class name starting with an uppercase letter.

命名表没有任何区别,只要您以单数形式命名文件和类,类名以大写字母开头。

You can use any of the following options:

您可以使用以下任一选项:

Table name:image_projects

表名:image_projects

File name:ImageProject.php

文档名称:ImageProject.php

Class name:ImageProject

班级名称:ImageProject



Table name:imageprojects

表名:imageprojects

File name:Imageproject.php

文档名称:Imageproject.php

Class name:Imageproject

班级名称:Imageproject



Table name:imageProjects

表名:imageProjects

File name:ImageProject.php

文档名称:ImageProject.php

Class name:ImageProject

班级名称:ImageProject

In this case you'll have to set the $tableproperty yourself.

在这种情况下,您必须$table自己设置属性。



Remember:If you don't name your class in singular form of what you name your table, you'll have to manually set it in your model:

请记住:如果您没有以您命名表的单数形式命名您的类,则必须在模型中手动设置它:

class ImageProjects extends Eloquent
{
    public $table = 'image_projects';
}

回答by daVe

Current Laravel version 4.2 table name convention works OK in that way:

当前的 Laravel 版本 4.2 表名称约定以这种方式工作正常:

Table name:image_projects

表名:image_projects

File name:ImageProject.php

文档名称:ImageProject.php

Class name:ImageProject

班级名称:ImageProject

The camel-case class name forced me with an exception to use table name underscores.

骆驼大小写的类名迫使我使用表名下划线作为例外。