php Laravel - 数据库、表和列命名约定?

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

Laravel - Database, Table and Column Naming Conventions?

phpdatabaselaravelnaming-conventionseloquent

提问by The Dude

I'm using laravel eloquent data objects to access my data, what is the best way to name my tables, columns, foreign/primary keys etc?

我正在使用 laravel eloquent 数据对象来访问我的数据,命名表、列、外键/主键等的最佳方法是什么?

I found, there are lots of naming conventions out there. I'm just wondering which one best suits for laravel eloquent models.

我发现,那里有很多命名约定。我只是想知道哪一种最适合 laravel eloquent 模型。

I'm thinking of following naming convention:

我正在考虑以下命名约定:

  1. Singular table names (ex: Post)
  2. Singular column names (ex: userId - user id in the post table)
  3. Camel casing for multiple words in table names (ex: PostComment, PostReview, PostPhoto)
  4. Camel casing for multiple words in column names (ex: firstName, postCategoryId, postPhotoId)
  1. 单一表名(例如:Post)
  2. 单列名称(例如:userId - post 表中的用户 ID)
  3. 表名中多个单词的驼峰式大小写(例如:PostComment、PostReview、PostPhoto)
  4. 列名中多个单词的驼峰式大小写(例如:firstName、postCategoryId、postPhotoId)

So with this, I could use similar syntax in the controller.

所以有了这个,我可以在控制器中使用类似的语法。

$result = Post::where('postCategoryId', '4')->get();

Are there any recommended Laravel guidelines for this? Can I proceed with these naming conventions?

是否有任何推荐的 Laravel 指南?我可以继续使用这些命名约定吗?

If someone has better suggestions, I will be very happy to hear them.Thanks a lot!

如果有人有更好的建议,我会很高兴听到他们的。非常感谢!

回答by user4055288

Laravel has it's own naming convention. For example, if your model name is User.phpthen Laravel expects class 'User' to be inside that file. It also expects userstable for Usermodel. However, you can override this convention by defining a table property on your model like,

Laravel 有它自己的命名约定。例如,如果您的模型名称是User.phpLaravel,那么 Laravel 期望类 'User' 位于该文件中。它还需要模型usersUser。但是,您可以通过在模型上定义表属性来覆盖此约定,例如,

    class User extends Eloquent implements UserInterface, RemindableInterface {
        protected $table = 'user';
    }

From Laravel official documentation:

来自 Laravel 官方文档:

Note that we did not tell Eloquent which table to use for our User model. The lower-case, plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the User model stores records in the users table. You may specify a custom table by defining a $tableproperty on your model

请注意,我们没有告诉 Eloquent 将哪个表用于我们的 User 模型。除非明确指定另一个名称,否则类的小写复数名称将用作表名。因此,在这种情况下,Eloquent 将假设 User 模型将记录存储在 users 表中。您可以通过$table在模型上定义属性来指定自定义表

If you will use user table id in another table as a foreign key then, it should be snake-case like user_idso that it can be used automatically in case of relation. Again, you can override this convention by specifying additional arguments in relationship function. For example,

如果您将在另一个表中使用用户表 id 作为外键,那么它应该是蛇形大小写,user_id以便在关系的情况下可以自动使用。同样,您可以通过在关系函数中指定附加参数来覆盖此约定。例如,

    class User extends Eloquent implements UserInterface, RemindableInterface {
        public function post(){
            return $this->hasMany('Post', 'userId', 'id');
        }
    }

    class Post extends Eloquent{
        public function user(){
            return $this->belongsTo('User', 'userId', 'id');
        }   
    }

Docs for Laravel eloquent relationship

Laravel 雄辩关系文档

For other columns in table, you can name them as you like.

对于表中的其他列,您可以随意命名它们。

I suggest you to go through documentation once.

我建议您查看一次文档。

回答by Seiji Manoan

I don't agree in general with these examples you both have shown right on here.

我总体上不同意你们俩在这里展示的这些例子。

It is clean if you take a look at the official Laravel documentation, especially in the Eloquent's relationship session (http://laravel.com/docs/4.2/eloquent#relationships).

如果您查看 Laravel 官方文档,就会很清楚,尤其是在 Eloquent 的关系会话中 ( http://laravel.com/docs/4.2/eloquent#relationships)。

Table names should be in plural, i.e. 'users' table for User model.

表名应该是复数,即用户模型的'users'表。

And column names don't need to be in Camel Case, but Snake Case. See it is already answered: Database/model field-name convention in Laravel?

并且列名不需要在 Camel Case 中,而是 Snake Case。看到已经回答了:Laravel 中的数据库/模型字段名称约定?

It is too usual you can see it is like the RedBeanORM: the Snake Case for columns, even if you try other one. And it is adviced to avoid repeating the table names with column ones due to the method you can call from the Model object to access their relationships.

太常见了,您可以看到它就像 RedBeanORM:列的 Snake Case,即使您尝试其他列也是如此。由于您可以从 Model 对象调用方法来访问它们的关系,因此建议避免将表名与列名重复。

回答by Ramesh

    class User extends Eloquent implements UserInterface, RemindableInterface {
        public function post(){
            return $this->hasMany('Post');
        }
    }

    class Post extends Eloquent{
        public function user(){
            return $this->belongsTo('User');
        }   
    }

回答by Martino

The default table naming conventions can easily cause conflicts with the installation of multiple packages who may have incidentally the same class names. A solution would be to name tables as: [vendor].[package].[class], which is in line with how namespacing in Laravel is applied.

默认的表命名约定很容易与多个包的安装发生冲突,这些包可能偶然具有相同的类名。一种解决方案是将表命名为:[vendor].[package].[class],这与 Laravel 中命名空间的应用方式一致。

Edited: Using dots in table names is not recommended though. Would there be an alternative convention to use to ensure developers of a modular built application do not need to worry about existing table names.

编辑:虽然不建议在表名中使用点。是否有替代约定可用于确保模块化构建应用程序的开发人员无需担心现有表名。