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

eugenefvdm's avatar

Database (model) inheritence

Taking the following database example of services, and a subset of services, e.g. hosting services, I have the following dilemma:

I create a main table, called 'services', and a model Service.

This table contains settings that are common to all services and those are:

*id *description *monthly_price

I create another table called 'hosting_services' and a model HostingService.

The reason why I create another table is because I would like to add other types of services in the future as well, for example GardeningService.

In the HostingService, I have some specific fields, e.g.:

*domain *disk_space *disk_space_used

How can I gracefully handle this in Laravel?

My research indicates multiple solutions but nothing solid. I also note from research here https://laravel-news.com/2016-survey/ that some people in the community are requesting "Better model inheritance support".

  1. Stack Overflow post describing Polymorphic Relations (PR) versus Single Table Inheritance (STI) https://stackoverflow.com/questions/23973850/model-inheritance-in-laravel#

  2. Example extending abstract classes.

  3. Link to Dauce's extension to Eloquent, noting issues with this extension. https://github.com/ThibaudDauce/EloquentInheritanceStorage

I'm really completely lost here and just need some pointers if this is do-able or if I should do normal belongsTo and hasMany instead which makes data summarizing and editing a lot more difficult.

0 likes
3 replies
bobbybouwmann's avatar
Level 88

I think polymorphic relations are the best option here. It's a deep dive, but if you understand how it works it really easy to work with.

If you find it hard to get started with polymorphic relations I would recommend you to take a documentation example and then slightly convert it to your code base and see how that works for you.

1 like
eugenefvdm's avatar

Thank you, I will give that a bash and report back in a few days from now once I have refactored my app.

EDIT:

It took only an hour to refactor my simple app to rather use polymorphic relations and this solved the problem.

Please or to participate in this conversation.