【问题标题】:Table naming convention for Laravel Eloquent hasManyThrough relationLaravel Eloquent hasManyThrough 关系的表命名约定
【发布时间】:2016-05-30 15:00:21
【问题描述】:

主实体Subject有多个CommentComment有多个Rate

我应该如何命名迁移表?

我在 Google 上搜索到 hasMany 关系的命名是 subjectssubject_comment

应该将Rate 表命名为subject_comment_ratesubject_comments_ratecomment_rate

【问题讨论】:

  • 如果您有另一个可以评论的模型,例如Post,我建议对Comment 使用多态关系。所以表名将是postssubjectscommentscommentablescomment_rates

标签: php laravel eloquent naming-conventions


【解决方案1】:

默认情况下,laravel 数据透视表遵循使用单数名称按字母顺序命名表的约定,因此如果您想遵循约定,您应该像这样命名您的表:

  • 主题
  • cmets
  • comment_subject
  • 费率
  • comment_rate

您可以偏离此约定,但您必须在设置关系时指定数据透视表的名称

【讨论】:

  • 如果我想避免过度标准化,我应该只使用subjectscommentsrates吗?
  • 是的,你可以有一个带有 subject_id 列的 cmets 表和一个带有 comment_id 列的 rate 表,然后主题模型可以通过 cmets 有多个速率
猜你喜欢
  • 2014-03-12
  • 2020-10-14
  • 2016-09-21
  • 2016-09-26
  • 2019-03-26
  • 2015-07-27
  • 1970-01-01
  • 2017-03-30
  • 2020-02-02
相关资源
最近更新 更多