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

RemizZ's avatar

Referencing multiple models

Hey there,

I just started developing with Laravel and of course I choose the most difficult project to start with... (probably not, but for me it is)

I'm trying to create a small page for managing accounts and login information for various services and types.

And therein lies the problem: I have 3 tables: domains, providers and projects. All of these can have a One-to-Many relationship with a table called accounts. The thing is, an account can never have more than one domain, project or provider associated with it, but the only way I see to connect all this is through pivot tables for every relationship type: account_domain, account_project, ... which would not be correct as I understand it, since pivot tables represent Many-to-Many relationships.

Am I missing something? Can someone point me in the right direction? Thanks :)

0 likes
5 replies
rdelorier's avatar
Level 9

Check out polymorphic relationships, your account can morph into one of the three other tables

1 like
RemizZ's avatar

Just what I was looking for, thanks! :)

RemizZ's avatar

Ok sadly I spoke too soon. I think I left out important information in my first post.

The thing is, one account can be related to either one or all of the other models, but with with polymorphic relationships I only have one entry in my accounts table, where the fields accountable_id and accountable_type are set to only relate to one other model. (i.e. App\Domain) How can I make it so an account can have a domain AND a project? What am I missing?

RemizZ's avatar

... this stuff is seriously messing with my brain. Thank you.

Please or to participate in this conversation.