In this tutorial you will learn about the Laravel One to Many Polymorphic Relationship Example and its application with practical example.
In this Laravel One to Many Polymorphic Relationship Example tutorial, I’ll show you how to implement laravel one to many polymorphic relationship and you will also learn how to use create, and retrieve records from database tables using this relationship.
Laravel One to Many Polymorphic Relationship Example
Developing any blogging portal application and you would have many tables like posts, videos and comments. You would need to add comments for posts and videos. In this scenario you can use One to Many Polymorphic Model Relationship in laravel. Because your comments model belongs to more than one model.
In Laravel “morphMany()” and “morphTo()” eloquent method allows you to create One to Many Polymorphic Relationship in your laravel eloquent models.
Create Migration File
In this example we will create posts, videos and comments migration files with following files:
posts migration file:
1 2 3 4 5 6 7 8 9 |
Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string("name"); $table->timestamps(); }); |
videos migration file:
1 2 3 4 5 6 7 8 9 |
Schema::create('videos', function (Blueprint $table) { $table->increments('id'); $table->string("name"); $table->timestamps(); }); |
comments migration file:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->string("body"); $table->integer('commentable_id'); $table->string("commentable_type"); $table->timestamps(); }); |
Create one to many polymorphic relationships in model
Now, we can create one to many polymorphic relationships as following:
Post Model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { /** * Get all of the post's comments. */ public function comments() { return $this->morphMany(Comment::class, 'commentable'); } } |
Video Model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Video extends Model { /** * Get all of the post's comments. */ public function comments() { return $this->morphMany(Comment::class, 'commentable'); } } |
Comment Model:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Comment extends Model { /** * Get all of the owning commentable models. */ public function commentable() { return $this->morphTo(); } } |
Retrieve, create a record using polymorphic relationship
Now you will learn how to retrieve and create a record from posts, videos and comments table using one to many polymorphic relationship:
Retrieve Records:
1 2 3 4 5 6 7 8 9 |
// for posts comments $post = Post::find(1); dd($post->comments); // for videos comments $video = Video::find(1); dd($video->comments); |
Create Records:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// for posts comments $post = Post::find(1); $comment = new Comment; $comment->body = "Hello world"; $post->comments()->save($comment); // for videos comments $video = Video::find(1); $comment = new Comment; $comment->body = "hello world"; $video->comments()->save($comment); |