Answer the question
In order to leave comments, you need to log in
How to store inherited Laravel models in DB?
Hello community. Tell me how to work with model inheritance using the example (User, Admin) or (User, Person, Organization) where Admin/Person/Organization are inherited from User. How to store them in the database? In particular, concise solutions in Laravel are of interest, but you can not get attached.
In theory, when accessing the property of the Admin model, I should also be able to access the properties from the User model. But how will they get there? Will you have to fill them in the constructor?
Also, when saving, will you have to write an event handler?
How does this process take place?
Also interesting are the solutions for the example that each type of object has different dashboards but at the same link (example.com/dashboard). How to do validation and bind appropriate controllers? Or do one controller, but call parent methods to implement different functionality?
In general, help raise the level.
welcome links
Answer the question
In order to leave comments, you need to log in
It is not necessary to consider a DB and models as something joint. This is a normal situation when several models refer to the same tables and their columns, depending on the context, the behavior may be different. martinfowler.com/bliki/BoundedContext.html
At the same time, models do not have to (but can) inherit from each other, it is better to replace inheritance with composition, that is, Admin/Person/Organization will have a method that returns User. Given Eloquent, the easiest and most clumsy way is a 1-1 relationship, something like
Admin/Person/Organization {
function user() { return $this->hasOne('User', 'id', 'id'); }
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question