实现多态多对多 几个注意点
1 定义中间表的时候 id 不能为主键
2 在定义与中间表关联的时候要注意传入第四个的外键 看下面是id
/**
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function tags()
{
return $this->morphToMany(Tag::class, 'tag','ba_tag_attach','id');
}
3 如果想改变中间表的type 模型定义可以在AppServiceProvider 这个服务下增加以下代码
Relation::morphMap([
'goods_online' => GoodsOnline::class
]);
最后的中间表设计
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: