Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RonB1985's avatar

What kind of relation would you choose?

Hi guys and gals,

So I'm working on my app and I am about to add a photo gallery and video gallery. I would like to keep the photos and videos seperate, but both will be using an Album to which I can add either videos, or photos. So, I will have 3 tables:

  1. Albums
  2. Photos
  3. Videos

I would just like to know is, how you YOU solve this "issue"? Personally I was thinking about a polymorphic relation first, but it would be some kind of reversed relation or something, so I dropped that idea. What I am currently thinking is, I create two pivot tables, album_photo and album_video, and add photos or videos to an album that way. Like that I can also use a photo or video in multiple albums. And in the Albums table I can specify wether or not it's a photo or video album.

Thoughts?

0 likes
2 replies
skovmand's avatar
skovmand
Best Answer
Level 3

I would just make a polymorphic relation. It will also let you store the same photo/video in multiple albums. Perhaps the table should be called album_items.

Then give the table the entries id - item_type - item_id - album_id - whatever you need - created_at - updated_at

1 like
RonB1985's avatar

Ah yes, so I was on the right track with the polymorphic relation, but I didn't think of it like that yet. Good solution, Ill see if that works for me too, thanks :)

Edit: Easy as pie, works like a charm, thanks again :)

Please or to participate in this conversation.