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

时间:2016-01-17 15:16:56

标签: php laravel eloquent

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

产品

  • id
  • product_number
  • description

用户

  • id
  • username

照片

  • id
  • path
  • imageable_id
  • imageable_type

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

谢谢,

1 个答案:

答案 0 :(得分:1)

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

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