Today we will look at how to use Laravel One To One Polymorphic Relationship, and learn how to apply it in a tutorial. So Which allows one model to relate to more than one other model on the same association.
The one-to-one polymorphic relationship is comparable to a simple one-to-one relationship; However, the target model can be of more than one type of model on the same association.
For example, imagine that users of your application can "comment" on both post and video. Using polymorphic relationships, you can use a single comment table for both of these usage cases.
Step 1 : Create Migrations
In this relationship, we have three database tables: posts, videos and comments. A profiles table will be related with posts and posts table.
posts table migration
php artisan make:migration create_posts_table
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('body');
$table->timestamps();
});
}
videos table migration
php artisan make:migration create_videos_table
public function up()
{
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
comments table migration
php artisan make:migration create_comments_table
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->text('body');
$table->integer('comment_id')->unsigned();
$table->string('comment_type');
$table->timestamps();
});
}
Now, let's look at the migration fields. We have comment_id and comment_type fields in the comments migration.
Migrate that schema to the database.
php artisan migrate
Step 2 : Create Models
we need 3 model so follow below 3 models
Comment Model
php artisan make:model Comment
Goto App/Models/Comment.php and edit bellow code
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
public function commentable()
{
return $this->morphTo();
}
}
Post Model
php artisan make:model Post
Goto App/Models/Post.php and edit bellow code
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Models\Comment;
class Post extends Model
{
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
Video Model
php artisan make:model Video
Goto App/Models/Video.php and edit bellow code
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Models\Comment;
class Video extends Model
{
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
You may want to use custom field name for "id" and "type" columns of polymorphic model. You need to ensure that you pass the name of the relationship as the first argument to the morphTo()
method. For that, you may use PHP's __FUNCTION__ magic constant. The second and third arguments are name of type and id fields like below:
public function profile()
{
return $this->morphTo(__FUNCTION__, 'comment_type', 'comment_id');
}
Step 3 : Get Records:
Get Commnets for post
/**
* Access Post Commnets.
*
* @return \Illuminate\Http\Response
*/
public function getCommnets()
{
$comments = Post::find(1)->comments;
dd($comments);
}
Get Commnets for Video
/**
* Access Video Commnets.
*
* @return \Illuminate\Http\Response
*/
public function getCommnets()
{
$comments = Video::find(1)->comments;
dd($comments);
}
Conversely, if you already have a record of the profile model and you want to retrieve the parent model, you can call the profile model's morphTo () method.
/**
* Access the parent model of profile.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$parent_model = Comment::find(2)->commentable;
dd($parent_model);
}
I hope this will help you to understand one to one polymorphic relationship in laravel app