Laravel 11 One to Many Polymorphic Relationship Example

Laravel 11 one to many polymorphic relationship example; Through this tutorial, i am going to show you one to many polymorphic relationship and how to use create, and retrieve records from database tables using this relationship in laravel apps.

In laravel using “morphMany()” and “morphTo()” eloquent method to create One to Many Polymorphic Relationship in your laravel eloquent models.

Laravel One to Many Polymorphic Relationship Example

Use the below given steps to create one to many polymorphic relationship in laravel apps; is as follows:

  • Step 1: Create Migration File
  • Step 2: Create one to many polymorphic relationships in model
  • Step 3: Retrieve, create a record using polymorphic relationship

Step 1: Create Migration File

Go to database/migration directory and inside this directory create posts, videos and comments migration file; is as follows:

posts migration file:

Schema::create('posts', function (Blueprint $table) {
 
    $table->increments('id');
 
    $table->string("name");
 
    $table->timestamps();
 
});

videos migration file:

Schema::create('videos', function (Blueprint $table) {
 
    $table->increments('id');
 
    $table->string("name");
 
    $table->timestamps();
 
});

comments migration file:

Schema::create('comments', function (Blueprint $table) {
 
    $table->increments('id');
 
    $table->string("body");
 
    $table->integer('commentable_id');
 
    $table->string("commentable_type");
 
    $table->timestamps();
 
});

Step 2: Create one to many polymorphic relationships in model

Now, create one to many polymorphic relationships in model; is as follows:

Post Model:

<?php
  
namespace App\Models;
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:

<?php
  
namespace App\Models;
  
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:

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Model;
  
class Comment extends Model
{
    /**
     * Get all of the owning commentable models.
     */
    public function commentable()
    {
        return $this->morphTo();
    }
}

Step 3: Retrieve, create a record using polymorphic relationship

To retrieve record from posts, videos and comments table using one to many polymorphic relationship:

// for posts comments
$post = Post::find(1);  
  
dd($post->comments);
 
// for videos comments
$video = Video::find(1);    
  
dd($video->comments);

To create a record from posts, videos and comments table using one to many polymorphic relationship:

// 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);   

Recommended Laravel Tutorials

Leave a Comment