【问题标题】:Using Other Column as Identifier in Polymorphic Relationship在多态关系中使用其他列作为标识符
【发布时间】:2016-01-17 23:43:59
【问题描述】:

我在我的项目中使用 Eloquent,我目前正面临与多态关系有关的问题。我想做的是在变形关系中指定另一列而不是主键,这样:

产品

  • id
  • product_number
  • description

用户

  • id
  • username

照片

  • id
  • path
  • imageable_id
  • imageable_type

在这种情况下,我想创建与 products 和 users 表的关系,但在 product 关系中,我不想将 ID 作为关系标识符,而是将 product_number 因为一些原因。从 5.1 版开始可以吗?

谢谢,

一月

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    可以像这样指定本地密钥:

    return $this->morphMany('App\Photos', 'imageable', null, null, 'product_number' );
    

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 2011-10-02
      • 2011-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多