使用其他列作为多态关系中的标识符


Using Other Column as Identifier in Polymorphic Relationship

我在我的项目中使用了Eloquent,我目前面临着关于多态关系的问题。我想做的是在变形关系中指定另一列而不是主键,以便:

产品

  • id
  • product_number
  • description

用户

  • id
  • username

照片

  • id
  • path
  • imageable_id
  • imageable_type

在这种情况下,我想与产品和用户表创建关系,但在产品关系中,由于某些原因,我想将 product_number ID 作为关系标识符,而不是将 ID 作为关系标识符。从 5.1 版开始是否可行?

谢谢

一月

可以通过指定本地键来实现,如下所示:

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