douhao9203 2016-02-28 20:03
浏览 164
已采纳

如何在Laravel 5.2中使用多对多的多态关系

I am reading laravel 5.2 docs to implement many to many polymorphic relation in my Laravel Application. I have Many models like Blog, Question, Photo etc and I want to have Tagging system for all of them. I have created Tag table with following schema

  Schema::create('tags', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('slug')->unique();
        $table->timestamps();
    });

Below is pivot table schema. Pivot table name is entity_tags

Schema::create('entity_tags', function (Blueprint $table) {
     $table->increments('id');
     $table->integer('tag_id')->unsigned();;
     $table->integer('taggable_id')->unsigned();
     $table->string('taggable_type');
     $table->timestamps();
     $table->index('tag_id');
     $table->index('taggable_id');
     $table->index('taggable_type');
});

This is the relationship defined in Tag model for Question model

public function questions()
{
    return $this->belongsToMany('App\Question', 'entity_tags', 'tag_id', 'taggable_id');
}

And the following relation is defined in Question Model

public function tags()
{
    return $this->belongsToMany('App\Tag', 'entity_tags', 'taggable_id', 'tag_id');
}

Now I want to define Many to Many Polymorphic relationship as defined in Laravel 5.2.
My Question are

  • how I can define them?
  • Should I remove the Many to Many relationship and only define Many to Many polymorphic relationship ? If yes, then how to manage custom pivot table name ?
  • Also is it required to suffix column name with word able that are part of polymorphic relationship ?
  • 写回答

1条回答 默认 最新

  • doumu4032 2016-02-29 07:36
    关注
    • Use return $this->morphToMany() instead of belongsToMany, and in the Tag model, write 3 methods with return $this->morphedByMany() for the reverse relationship.

    • You only need polymorphic definitions, no need for the many to many normal ones. The name of the pivot table is with 'able' at the end by the default convention but you can name it anything you want.

    • no, you don't have to have a word with 'able' at the end, it's just a way to define that it's something more general, you can name it anything you want.

    Naming is based on some default convention by Laravel.

    Update:

    You have the following pivot table schema:

    Schema::create('entity_tags', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('tag_id')->unsigned();;
            $table->integer('entity_id')->unsigned();
            $table->string('entity_type');
            $table->timestamps();
            $table->index('tag_id');
            $table->index('entity_id');
            $table->index('entity_type');
    });
    

    and the tags table:

    Schema::create('tags', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('slug')->unique();
        $table->timestamps();
    });
    

    So you want to create the relationships for blog, video and question tables / models:

    Tag.php Model:

    public function questions()
    {
        return $this->morphedByMany('App\Question', 'entity', 'entity_tags');
    }
    
    public function blogs()
    {
        return $this->morphedByMany('App\Blog', 'entity', 'entity_tags');
    }
    
    public function videos()
    {
        return $this->morphedByMany('App\Video', 'entity', 'entity_tags');
    }
    

    Question.php / Blog.php / Video.php

    public function tags()
    {
        return $this->morphToMany('App\Tag', 'entity', 'entity_tags');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示