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');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路