Laravel 5: Eloquent – relationship types: many to many polymorphic relations

Eloquent: relationship types – many to many polymorphic relations

Difference between one-to-one polymorphic relations and many-to-many polymorphic relations – see here.
How to prepare models to be able to easily access and store data in tables.
This page describes practical use of methods:
  • morphToMany and
  • morphedByMany

Just scroll down.

Important!!!
Go here and read about morphMap  and ‘gotchas
This is in case, you will do everything on this page and result is going to come empty.

First DB tables used in examples.

Main table: messages (Messages model)

Pivot table: message_attachables

Related table sample: tutor_aids (TutorAid)

morphedByMany

You are looking for all Tutor Aids that are related (linkd to) selected message.

This method should be located on main model – in this case Messages,
as you are looking for related data for selected message.

example:

First argument is what you want to fetch – in this case: TutorAid::class.
Second ‘message_attachable’ is a prefix of message_attachable_id and message_attachable_type.
It allows to identify table and some other stuff.

usage example:

usage explained:

You are selecting message first by primary id: Message::find(2);

Now, you are looking for all TutorAids: ->getTutorAids

that are matched with model TutorAids in pivot table message_attachables.

Look at table message_attachables
Message with message_id = 2 that is marked by model TutorAid is listed twice.

morphedByMany will now look into \App\Models\Tutor\TutorAid::class (table tutor_aids)
using message_attachable_id data (which matches primary id in TutorAid) and fetch for us details of these tutor aids.

Additional data that can be fetched

Collection comes here with some additional data, like:

  • pivotParent: message data (table messages, model Message) and
  • pivot table data (table message_attachables, Model MessageAttachables)

Access message data:

note: getTutorAids[0]

Collection comes usually as an array, so you either loop it, or access it using key,
to specify which you are interested in.

Using process describes above, you can define Message relation to other models
from column message_attachable_type like:

  • TutorContent
  • TutorTest

… and create methods on Message model:

  • getTutorContents()
  • getTutorTests()

 

morphToMany
This method is placed in models listed in column message_attachable_type:
  • TutorContent
  • TutorTest
  • TutorAid

This method allows to work in reverse and will get us all mesages (model Message)
that are related to one of three models listed above.

Here is morph method placed in TutorAids model:

As you see, first argument is different, than in morphedByMany, as we are looking now for related messages.

usage example:

Just to explain above code – we are getting all messages for TutorAid #538.

If you look into sample table: message_attachables in column: message_attachable_id,
you will find TutorAid #538 twice.
Related message_id are: 2 and 194.

Now morphToMany will look into Model Message (table messages) and fetch for us
two messages with primary id: 2 and 194.

sample data fetched:

Also, we can get pivotParent (tutor_aids) and pivot (message_attachables) tables data.

Now, you can repeat this process on models:

  • TutorContent
  • TutorTest

and get methods getMessages related to those models.