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

Laravel - Database, Table and Column Naming Conventions?(Laravel - 数据库、表和列命名约定?)
本文介绍了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 - 数据库、表和列命名约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)