How to use many, many polymorphic relationships in Laravel 5.2

I am reading larvel 5.2 docs for implementing many, many polymorphic relationships in my Laravel application. I have many models, such as Blog , Question , Photo , etc., and I want to have a tag system for all of them. I created a tag table with the following diagram

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

Below is a pivot table diagram. entity_tags pivot table 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 relationship is defined in the Tag model for the Question model.

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

And the following relationship 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, many polymorphic relationships, as defined in Laravel 5.2.
My question

  • How can I identify them?
  • Should I remove many of the many? relationships and identify only a lot of polymorphic relationships? If so, how do I manage the custom name of the pivot table?
  • Also requires a suffix for the column name with the word able , which are part of a polymorphic relationship?
+6
source share
1 answer
  • Use return $ this-> morphToMany () instead of applyToMany, and in the Tag model write 3 methods with $ this-> morphedByMany () return for feedback.

  • You only need polymorphic definitions, you donโ€™t need many, many normal ones. The name of the pivot table is โ€œcapableโ€ at the end by default, but you can name anything.

  • no, you donโ€™t need to have a word with โ€œcapableโ€ in the end, it's just a way to determine that it is something more general, you can call it anything.

Naming is based on the standard Laravel convention.

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 tag table:

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

So you want to create relationships for your blog, videos, and magazines / 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'); } 
+4
source

All Articles