Laravel - 如何设置 morphOne 关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43771242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:54:30  来源:igfitidea点击:

Laravel - How to setup a morphOne relationship

laraveleloquentpolymorphismrelationship

提问by Big Boss

I'm trying to implement a morphable table for categories, right now I've the following.

我正在尝试为类别实现可变形表,现在我有以下内容。

// Snippet Table
- id
- title
- body

// Post Table
- id
- title
- body

// Category Table
- id
- name

I want to be able to morph the posts and snippets to have only one category, something like this:

我希望能够将帖子和片段变形为只有一个类别,如下所示:

// Categorizable Table
- category_id
- categorizable_id
- categorizable_type

Do I have to have another model for this categorizable table? Or there is a way to set it without another model?

我是否必须为这个可分类表提供另一个模型?或者有一种方法可以在没有其他模型的情况下进行设置?

So far I have this

到目前为止我有这个

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

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

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

And I have 4 tables, snippets, posts, categories, categorizables, the last one the migration looks like this.

我有 4 个表,snippets, posts, categories, categorizables,最后一个迁移看起来像这样。

Schema::create('categorizables', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('category_id');
    $table->morphs('categorizable');
    $table->timestamps();
});

The thing is, what is the correct way to save this relationship, I'm testing it on tinker and bothattach($category_id)and sync($category_id)aren't saving the relationship they return the error BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::categorizable()', What I'm missing?

问题是,保存这种关系的正确方法是什么,我正在 tinker 和两者上进行测试,attach($category_id)并且sync($category_id)没有保存他们返回错误的关系BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::categorizable()',我错过了什么?

回答by yazfield

Sorry I thought you wanted many-to-many relationship. morphOnerelationship API is the same as morphManyfrom the docs

抱歉,我以为你想要多对多的关系。morphOne关系 APImorphMany文档中的相同

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

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

EDITWhen using morphOne you don't need a pivot table categorizablestable must be deleted and change your categoriestable to include the morph fields

编辑使用 morphOne 时,您不需要数据透视categorizables表必须删除并更改您的categories表以包含变形字段

// Category Table
- id
- name
- categorizable_id
- categorizable_type