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 ?