我正在做一个有一些产品的项目。这些产品中的每一个都属于一个类别。现在我必须向我的项目添加一个帖子部分。每个帖子都属于一个明显不同于产品类别的类别。我想知道是否有办法将该现有类别用于帖子,我必须创建一个不同的模型(例如 PostCategory(。
你可以从这里获得帮助
产品型号
public function categories()
{
return $this->morphMany('AppCategory', 'categorizable');
}
帖子模型
public function categories()
{
return $this->morphMany('AppCategory', 'categorizable');
}
类别模型
public function categorizable()
{
return $this->morphTo();
}
你的类别表应该是这样的
categories (table)
id - integer
body - text
categorizable_id - integer
categorizable_type - string
在此处查看详细信息 https://laravel.com/docs/5.6/eloquent-relationships#polymorphic-relations