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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?