Laravel 10 One to Many Polymorphic Relationship Example

Laravel 10 One to Many Polymorphic Relationship Example

Laravel 10 one to many polymorphic relationship example; In this tutorial, you will learn about laravel one to many polymorphic relationship and how to use create, and retrieve records from database tables using this relationship.

When you work with any blog post laravel application and you have many tables like posts, videos and comments.

And you need to add comments for posts and videos. So this time you can use One to Many Polymorphic Model Relationship. Because your comments model belongs to more than one model.

Note that, using “morphMany()” and “morphTo()” method, you can create One to Many Polymorphic Relationship in your laravel eloquent models.

Laravel One to Many Polymorphic Relationship Example

Follow the following steps to create one to many polymorphic relationship and learn how to use this relationship:

Step 1: Create Migration File

First of all, create posts, videos and comments migration files with following files:

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

Next, create one to many polymorphic relationships as follow:

Post Model:

<?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:

<?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:

<?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();
    }
}

Step 3: 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:

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

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

Create Records:

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

Conclusion

In this tutorial, you have learned how to create and use one to many polymorphic relationship in laravel apps.

Recommended Laravel Tutorials

Recommended:-Laravel Try Catch

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

One reply to Laravel 10 One to Many Polymorphic Relationship Example

  1. Thanks for sharing information. keep sharing

Leave a Reply

Your email address will not be published. Required fields are marked *