1 month ago

Morph to relationship

Posted 1 month ago by kiasaty


I have these tables:

users, articles, images.

images could be for users, or articles, or none. I mean some images have no foreign_key. they have no relationships.

there are 3 solutions in my mind:

  1. use an eloquent Polymorphic Relationship, with nullable imageable_id and imageable_type columns. is this even possible?

  2. don't use a foreign_key in the images table. instead, create these pivot tables:

  • a "user_image" table with columns: user_id, image_id.
  • a "article_image" table with columns: article_id, image_id.
  1. do the previous method, but instead of using to pivot tables, use one pivot table with these columns: imageable_id, imageable_type

Please sign in or create an account to participate in this conversation.