Laravel - 数据库,表和列命名约定? [英] Laravel - Database, Table and Column Naming Conventions?

查看:892
本文介绍了Laravel - 数据库,表和列命名约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用laravel雄辩的数据对象来访问我的数据,什么是命名我的表,列,外键/主键等的最好方法?

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?

发现,有很多命名约定。

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

我正在考虑以下命名惯例:

I'm thinking of following naming convention:


  1. 奇异的表格名称(例如:Post)

  2. 奇异的栏位名称(例如:userId - 讯息表格中的使用者ID)

  3. 表格名称中多个字词的驼峰套管(例如:PostComment,PostReview,PostPhoto)

  4. 列名称中多个字词的驼峰套管(例如:firstName,postCategoryId,postPhotoId)

  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)

因此,我可以在控制器中使用类似的语法。

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

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

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

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

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

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

推荐答案

Laravel有自己的命名约定。例如,如果你的模型名是 User.php ,则Laravel要求类'User'在该文件内。它还期望用户表中的用户模型。但是,您可以通过在模型上定义一个表格属性来覆盖这个约定,例如

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

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

从Laravel官方文档:

From Laravel official documentation:


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

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 $table property on your model

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

If you will use user table id in another table as a foreign key then, it should be snake-case like user_id so 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,

    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');
        }   
    }

用于Laravel雄辩关系的文档

对于表中的其他列,您可以根据自己的需要命名。

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

我建议你一次查看文档。

I suggest you to go through documentation once.

这篇关于Laravel - 数据库,表和列命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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