Laravel - How to setup a morphOne relationship

99封情书 提交于 2019-12-04 12:20:52

Sorry I thought you wanted many-to-many relationship. morphOne relationship API is the same as morphMany from the docs

Post extends Model
{
    public function category() {
        return $this->morphOne(Category::class, 'categorizable');
    }
}

Category extends Model
{
    public function categorizable() {
        return $this->morphTo();
    }
}

EDIT When using morphOne you don't need a pivot table categorizables table must be deleted and change your categories table to include the morph fields

// Category Table
- id
- name
- categorizable_id
- categorizable_type
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!