Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Model Inheritance in Laravel

Tags:

i'm currently working with Laravel and I'm struggling with the fact that every model needs to extend from Eloquent and I'm not sure how to implement a model Hierarchy (with different tables)

For Example:

Let's say I have an abstract model Tool, and then a Model Hammer and a Model Screwdriver that extend from Tool.

Now, Tool would extend Eloquent ... BUT, there is NO Table for Tools, there is a table for Hammers and another Table for Screwdrivers, because they have different attributes.

How do I specify that Hammer has a table and Screwdriver has a table when they both extend Tool? And how can I use Eloquent to call, For Example, All Tools?

Like:

Tools::all() 

This Should bring all Hammers and Screwdrivers because they are all Tools

Is this possible using Eloquent?

like image 784
Gabriel Matusevich Avatar asked May 31 '14 19:05

Gabriel Matusevich


People also ask

What is inheritance in Laravel?

While inheritance is one of the pillars of object oriented programming, it's actually less beneficial when used alongside a framework like Laravel. One of the drawbacks is that you inject a layer of separation between your code and Laravel. Ironically, this separation is often the motivation for using inheritance.

What is inheritance in PHP with example?

Inheritance in OOP = When a class derives from another class. The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods. An inherited class is defined by using the extends keyword.

How does model work in Laravel?

Laravel Create Model is an MVC based PHP system. In the MVC architecture, 'M' stands for 'Model'. A model is used as a way for questioning data to and from the table within the database. Laravel gives a basic way to do that using Eloquent ORM where each table incorporates a Model to interact with it.

What are Laravel Mutators?

Accessors and mutators allow you to format Eloquent attributes when retrieving them from a model or setting their value. For example, you may want to use the Laravel encrypter to encrypt a value while it is stored in the database, and then automatically decrypt the attribute when you access it on an Eloquent model.


1 Answers

Even though I like the Polymorphic Relations (PR) approach slightly better than the Single Table Inheritance (STI), it still does not feel anything like a true inheritance approach. From the domain (e.g. a UML Class Diagram) point of view, it is like trying to use a composition relationship instead of inheritance.

From a database consistency point of view the polymorphic relation by itself is already a very weird solution in Laravel (IMHO). As there can be no single field that is a foreign key to multiple tables, this can lead to joining ID's that should not be joined. And inverse relationships that are not obeyed.

Although I'm not actually describing a solution to the problem, I'd propose a different approach than PR and STI. This solution would be similar to Hibernate's table-per-subclass approach. I think that Dauce's extension to Eloquent is going in the same direction, except there seem to be a few implementation issues still.

From a database point of view, a table per subclass would also mean that the super-class contains one column per direct subclass. Then you can put foreign key constraints on the (non-null) id's and properly use the relationships. However, there should still be some extra logic in Laravel's magic Eloquent class that turns the requested object into the right type.

So for me, functionally, the Eloquent models should properly inherit on the PHP side, while the database can still use the foreign key constraints.

like image 193
Joost Avatar answered Sep 26 '22 22:09

Joost