@igaster You might want to look into polymorphic relations. The fact of the matter boils down to: OOP inheritance does not map directly to a relational database system like MySQL.
Feb 15, 2016
11
Level 11
Laravel 6 Feature: Eloquent Inheritance
True multitable inheritence in Eloquent. In a perfect world I could:
class User extends Eloquent{
// table 'users': common columns for all users
}
class Student extends User{
// table 'students': adds columns specific to students
// key 'user_id' points to parent object
}
class Teacher extends User{
// table 'teachers': adds columns specific to teachers
// key 'user_id' points to parent object
}
In this perfect world Laravel would abstract the database structure from the child models. For example:
User::create([
'name' => 'Joe', // table: users.name
'class' => 'C', // table: students.class
)];
Teachers::create([
'name' => 'Albert', // table: users.name
'lesson' => 'maths', // table: teachers.lesson
)];
Student::find(1); // LEFT JOIN between users and students
Teachers::where('lesson', 'algebra')->get(); // Got the point?
This functinality would require that Laravel has knowledge about the table's structure so that it can distinguish between their attributes and handle conflicts. (this is the reason for Laravel 6 title!)
Doctrine has a basic implementation, but far from perfect...
Why do I need this:
- DRY database & models
- Proper normalize DB
- DB Performance (?)
- OOP inheritance = DB inheritance = Developer happiness
By the way, I've already tried to simulate igaster/eloquent-inheritance but itis not a true solution..
Looking for your thoughts!
Please or to participate in this conversation.