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?
source share