S
S
Stanislav Pochepko2016-05-27 10:14:20
MySQL
Stanislav Pochepko, 2016-05-27 10:14:20

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

2 answer(s)
V
Vyacheslav Plisko, 2016-05-27
@AmdY

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'); }
}

D
D', 2016-05-27
@Denormalization

Why use different models?
For this purpose, you need to use the mechanism of roles.
And for the role mechanism, you can use https://github.com/Zizaco/entrust
It makes no sense to fence separate models for each role.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question