Laravel 5: Eloquent – relationship types: DB data used in examples

Eloquent: relationship types – DB data used in examples


Each sample table has just a few lines to demonstrate structure and help understand relationship.

users table:

phones table:

comments table:

posts table:

tags table:

post to tag pivot table:

ebooks table:

readers table:

comments table:

sellers table:

many-to-many polymorphic relation (pivot table):

Important about this pivot table!

  1. Name of this pivot table should be: ‘saleables
  2. Name of table comes from prefix of columns: ‘saleable_id’, ‘saleable_type’ -> ‘saleable‘, and is a plural form of this prefix, so ‘saleables
  3. prefix, as in these 2 columns above, must be same as second argument in morph methods, like: morphMany, morphToMany, morphedByMany
    example: