4 years ago

Many to many Polymorphic with additional identifiers

Posted 4 years ago by alexleonard

I haven't started coding this because I'm having a slightly difficult time wrapping my head around what would likely be the best approach.

So, I want to have say Portfolio and Page models. Both of these models can access a single Photo to use in SEO meta (for twitter sharing etc), but the Portfolio also needs to have a 'featured' photo which will be used as a thumbnail, but then can also have multiple photos to be displayed in a gallery.

I'm pretty sure I can set up the general morph stuff (although perhaps I'm still thinking in a flat polymorphic structure, not a many to many polymorphic structure) so that the Portfolio can have multiple images related to it, but how do I handle identifying a specific image as the 'featured' image vs the gallery images.

Ultimately I'd want to be able to do



@foreach($portfolio->galleryPhotos as $photo)

I'm not sure out of the box that many to many polymorphism will do it, I seem to end up with a default photoables table like

| id | photo_id | photoable_id | photoable_type |
|  1 |        1 |            1 | App\Meta       |

I guess I could add a portfolio_id field to the photos table and set up an additional hasOne relationship, but that sort of feels like I'm not doing it the right way..

Any advice hugely appreciated.

Thanks! Alex

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