Laravel Polymorphic Many-to-Many relationship pivot table with relationship to another Model

I have the following table structure as shown in the diagram:
database diagram

Briefly, it is composed of several many-to-many polymorphic relationships as described:

  • many resources can have many sources and the pivot table sourceables contains catalog_number and lot_number information to make each row in the pivot table unique. Many resources could also come from the same source or from different sources, differentiated by the catalog number and lot number on the pivot table.

  • many resources can also have many publications attached to it, through the publicationables table with notes on the pivot table

  • a resource’s source could also be described in many publications.

My questions:

  1. Since the resource’s source is differentiated by the pivot table sourceables how should I save the relationship between the pivot rows of sourceables to the publications?
  2. How to retrieve a resource with all it’s publications and also with the sources with all corresponding publications?

Source: Laravel

Leave a Reply