Laravel 5: Eloquent – relationship types: polymorphic relations

Eloquent: relationship types – polymorphic relations

DB tables used in examples: here.
Tables you should look for:
— ebooks table (Ebook model)
— readers table (Reader model)
— comments table (Comment model) (quasi-pivot)

polimorphic

Lets explain idea behind polymorphic relations

Laravel’s polymorphic relations are actually, what I did many times over in my projects.
I just did not call them so dramatically.

Figure A: my old way

You have Products and Services.
Both stored in separate tables.
I could have each assigned separate Comments table.
But that would require joining tables for any common output of comments for both.
So, what I did, I used single table looking something like this (simplified):

In above table

  • type_id was a foreign_key for a table specified in
  • type column.

Figure B: Laravel polymorphic relation

Now, lets look at Laravel polymorphic relation table structure:

See similarity?

Above table has also 2 relevant columns:

  • commentable_id column holds foreign_key for
  • table attached to model defined in commentable_type column

Conclusion

So as you see – by comparing both tables, this hifi name – polymorphic relation – is just another spirit killing off the high shelf jargon.
While in reality, the whole idea is a simple stuff, that’s been around for ages.

What needs to be explained in detail, is how to use it inside of Laravel framework.

Gotchas – must be taken care of – or your coiffure may get damaged!!!

REMEMBER !!

As of Laravel 5.3 you MUST setup morphMap array.
If you do not do this, Laravel will not properly process your Model names from column ‘commentable_type‘.
In commentable_type, you have just their name, while Laravel needs them as fully qualified name (with namespace), like:

So, unless you want to store fully qualified name, you need morphMap.

If you do not set morphMap, then all comments calls will fail quietly – very frustrating!
And all calls for ‘commentable‘ will return that your model from ‘commentable_type‘ field cannot be found.

How to set it:
To boot() method of AppServiceProvider add this code (except for ‘use‘ that goes on top):

Of course ‘App\’ section must be your true namespace to your model.

In Laravel 5.2 you had $morphClass property which you used in Model like this:

Not anymore.

Polymorphic relations Models

I am going to use these three tables:

  • comments
  • ebooks
  • readers

Models below will have simplified relations without all attributes.
Just see this file for more (it seems to be cleaner than API):

Comment model

This is a main model that holds data common to many models and has foreign keys and model names columns in its table, like commentable_id and commentable_type.

Reader model

This is one of related model, that has some data in table overseen by main model.

Ebook model

This is one of related model, that has some data in table overseen by main model.

How to handle data in polymorphic relations
get model instance of related model by using its foreign key in polymorphic model column ‘commentable_id’

get comments for given related model by using its foreign key in polymorphic model column ‘commentable_id’

Save comment for Ebook, or Reader