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

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

问题描述

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

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.

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

I'm thinking of following naming convention:

  1. 单一的表名(例如:Post)
  2. 单列名称(例如:userId - 帖子表中的用户 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();

是否有任何推荐的 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' 位于该文件中.User 模型还需要 users 表.但是,您可以通过在模型上定义表属性来覆盖此约定,例如

Laravel has its 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将假设 User 模型将记录存储在 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 eloquent关系的文档

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

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

我建议您浏览一次文档.

I suggest you to go through documentation once.

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

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