Many To Many Polymorphic Relationships In Laravel
Lets start.
How to define Many To Many Polymorphic Eloquent Relationship
1.Create laravel project and connect to database in .env file.
composer create-project laravel/laravel blog --prefer-dist
2.Create Model and Migrate it.
To implement Many To Many Polymorphic Relationships in Laravel, we need models: Posts,Video,tag
and taggables
.
Now we need to create Posts,Video,tag
and taggables
models in fresh project so for that run below commands in comand prompt.
php artisan make:model Post -m
php artisan make:model Video -m
php artisan make:model tag -m
php artisan make:model taggable -m
3.After Created Models we need to Implement code in Model and migration files as below
Migration files will be in database folder
2020_09_08_111619_create_posts_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('posts');
}
}
2020_09_08_132636_create_videos_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVideosTable extends Migration
{
public function up()
{
Schema::create('videos', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('videos');
}
}
2020_09_08_043710_create_tags_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTagsTable extends Migration
{
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('tags');
}
}
2020_09_08_043825_create_taggables_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTaggablesTable extends Migration
{
public function up()
{
Schema::create('taggables', function (Blueprint $table) {
$table->integer('tag_id');
$table->integer('taggable_id');
$table->string('taggable_type');
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('taggables');
}
}
Models files will be in app folder
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'name'
];
public function tags()
{
return $this->morphToMany('App\Tag', 'taggable');
}
}
video.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Video extends Model
{
protected $fillable = [
'name',
];
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
Defining The Inverse Of The Relationship
Next, on the Tag
model, you should define a method for each of its related models. So, for this example, we will define a posts
method and a videos
method:
tag.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class tag extends Model
{
protected $fillable = ['name'];
public function posts()
{
return $this->morphedByMany('App\Post', 'taggable');
}
public function videos()
{
return $this->morphedByMany('App\Video', 'taggable');
}
}
Retrieving The Relationship
Once your database table and models are defined, you may access the relationships via your models. For example, to access all of the tags for a post, you can use the tags
dynamic property:
$post = App\Post::find(1);
4.Create route and controller to access data.
Route in Web.php
Route::get('manytomanypoly','PolyController@manytomanypoly');
Create PolyController.php using below command in command prompt.
php artisan make:controller PolyController
Implement code in PolyController.php as below to access data
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Tag;
use App\Video;
use App\Post;
class PolyController extends Controller
{
public function manytomanypoly()
{
$tag = App\Tag::find(1);
foreach ($tag->videos as $video) {
print_r($video);
}
}
}