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

thelbig's avatar

Relation between an Object, a Container and ContainerOptions

Hi there,

i'm trying to solve the following problem:

Scenario: There is a Container Class, which contains several (1-n) ContainerOption Objects. These ContainerOptions should be assignable to multiple Entities in the application, but every Entity is only allowed to choose one ContainerOption per Container. (E.g. A User may assign one Tag (ContainerOption) per TagCategory (Container) to Images or Articles (Entity) etc.)

I started to create a polymorphic Relationship between ContainerOption and the Entity. The Problem with this approach, is that i can assign multiple ContainerOptions from the same Container to my Entity.

The next approach would be adding the ContainerId to the Entity/ContainerOption pivot table and declare the combination of these three fields as unique, but I'd have to change Eloquents behavior on the polymorphic relations. Besides, i think that wouldn't be a good approach.

I could avoid assigning multiple ContainerOptions from one Container to an Entity on the PHP-side, but i don't think thats a clean solution...

I think there might be a clean solution - has anyone else faced this problem before?

0 likes
0 replies

Please or to participate in this conversation.