Can the Type belong to any other model? If not, then it isn't Polymorphic, just a standard One to Many.
Can you show us your schema design and give any more information so we can better inform you.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello,
I am just designing my data model and came upon a special case, where I'm not sure which kind of relationship is the best choice.
We have a model Installation which has an unique id and a type (one of 6 possible types), and which is the basis for our billing system. Each Installation then has to have some details which depend on the type, i.e Heat, Power, Water,... , which include some specialized technical, organisational, and even billing informaton.
When the Installation is of type Heat then the details can be found in the table install_heat; when it is Power in install_power, when it is Water in install_water.
So the schema looks something like this:
installations
id - unique integer, primary key
type - integer (1 for Heat, 2 for Power, 3 for Water, ...)
... other stuff
install_heat
id - primary key
installation_id - foreign key to installations table
... other stuff specific to Heat installations
install_power
id - primary key
installation_id - foreign key to installations table
... other stuff specific to Power installations
install_water
id - primary key
installation_id - foreign key to installations table
... other stuff specific to Water installations
As you can see, the install_* tables just provide some specialized details for an Installation. So it basically is a One-to-One, but where the second side can reside in any of the install_* tables. That's why I was thinking of this being polymorphic.
I currently plan to to it this way:
class Installation extends Model
{
public function details()
{
return $this->morphTo();
}
}
and for the installation types:
class InstallType1 extends Model
{
public function main()
{
return $this->morphOne(Installation::class, 'details');
}
}
What do you think, is this a good way to go? Or am I missing something crucial?
Thanks in advance for any comments!
Edit: clarified the setup.
@jst I think I would go for a morph relation. I believe it is the cleanest solution for this.
Please or to participate in this conversation.