2 Comments

Eloquent polymorphic relationships explained (with examples)

In my opinion, Eloquent is one of the most powerful features of Laravel. It is an API for interacting with your database, and it has a very nice and easy-to-remember syntax. For example:

$post->author->name;

Will give you the name of the post's author.

This is an example of an Eloquent relationship. Relationships define how your models (tables) are connected. Although most are easy to understand, there are a few more complicated ones.

In this post, I'm going to show how every polymorphic relationship works. You can read more about "default" relationships here, and click here if you want to learn how to unit test your relationships.

One to one

In this example, we have three models: a Post, a Video, and an Image.

  • A Post has one Image
  • A Video has one Image
  • An Image belongs to a Post or Video

And we have this table structure:

posts
    id - integer
    title - string

videos
    id - integer
    name - string

images
    id - integer
    path - string
    imageable_id - integer
    imageable_type - string

We can define these relationships like this:

// app/Models/Post.php

public function image()
{
    return $this->morphOne(Image::class, 'imageable');
}
// app/Models/Video.php

public function image()
{
    return $this->morphOne(Image::class, 'imageable');
}
// app/Models/Image.php

public function imageable()
{
    return $this->morphTo();
}

Now we can access the image like this:

$post->image->path;
$video->image->path;

And if we have the $image, we can get the object where it belongs to (a Post or a Video) like this:

$image->imageable;

One to many

In this example, we have three models: a Post, a Video, and a Comment.

  • A Post has many Comments
  • A Video has many Comments
  • A Comment belongs to a Post or a Video

And we have this table structure:

posts
    id - integer
    title - string

videos
    id - integer
    name - string

comments
    id - integer
    body - string
    commentable_id - integer
    commentable_type - string

We can define the relationships like this:

// app/Models/Post.php

public function comments()
{
    return $this->morphMany(Comment::class, 'commentable');
}
// app/Models/Video.php

public function comments()
{
    return $this->morphMany(Comment::class, 'commentable');
}
// app/Models/Comment.php

public function commentable()
{
    return $this->morphTo();
}

Now we can access the comments like this:

foreach($post->comments as $comment) {
    //
}

foreach($video->comments as $comment) {
    //
}

And if we have a $comment, we can get the corresponding model (a Post or a Video) like this:

$comment->commentable;

Many to many

In this example, we have three models: a Post, a Video, and a Tag.

  • A Post has many Tags
  • A Video has many Tags
  • A Tag belongs to many Posts or Videos

For example, a Tag called "personal" can belong to a Post and a Video.

We may have this table structure:

posts
    id - integer
    title - string

videos
    id - integer
    name - string

tags
    id - integer
    name - string

taggables
    tag_id - integer
    taggable_id - integer
    taggable_type - string

We can define the relationships like this:

// app/Models/Post.php

public function tags()
{
    return $this->morphToMany(Tag::class, 'taggable');
}
// app/Models/Video.php

public function tags()
{
    return $this->morphToMany(Tag::class, 'taggable');
}
// app/Models/Tag.php

public function posts()
{
    return $this->morphedByMany(Post::class, 'taggable');
}

public function videos()
{
    return $this->morphedByMany(Video::class, 'taggable');
}

Now we can access the tags like this:

foreach($post->tags as $tag) {
    //
}

foreach($video->tags as $tag) {
    //
}

And if we have a $tag, we can access the posts and videos like this:

foreach($tag->posts as $post) {
    //
}

foreach($tag->videos as $video) {
    //
}

Share this article:

Subscribe to my newsletter

Continue reading:

Understanding the magic of Laravel macros

Using Laravel macros is a powerful way to extend default behavior of many classes in Laravel, such as Collections, Stringables and Reponses. In this article I...

How to convert PHPunit to Pest PHP

Pest is a new but great PHP testing framework. It has a simple and elegant syntax and a few nice features build on top of PHPunit. In this article, I'll show...

The TALL stack explained

The TALL stack is a set of frameworks to build interactive apps using Laravel. It stands for Tailwind CSS, Alpine.JS, Laravel, and Livewire. In this article,...

Leave a comment

Comments (2)

    Mahdi 's avatar
    Mahdi Reply
    6 months ago

    Thanks so much

      Jeroen van Rensen's avatar
      Jeroen van Rensen
      5 months ago

      Glad to help!