Single-Table Inheritance with Laravel
HTML-код
- Опубликовано: 11 июн 2021
- 👨💻 Learn Test-Driven Development with Laravel!
tddwithlaravel.com
Sign up to 30 Days of Laravel 👉🏻 30daysoflaravel.com
Parental by Caleb Porzio: github.com/calebporzio/parental
👨💻 Sign up to my newsletter and receive PHP, JS and Laravel news in a weekly-basis:
subscribe.mateusguimaraes.com
🎉 Party up:
/ mateusjatenee
/ mateusjatenee
/ mateusguimaraes
Thanks for watching!
you are the best, until now no one had explained this in much time and you did it super well, as well as the syntax of laravel. . Many Thank you so much thanks for the video. I would like a video with multi - table inheritancethat I can not find any explanation yet
Really awesome explanation. Looking forward for episode 2.
Mateus, thanks for the great video. His explanation was so clear that it made it easy to understand a complex topic like this! I'm looking forward to the next video!
Fantastic explanation! Opened my mind.
Love this, it seems like such a cool pattern, I can't wait to try it out.
This was very helpful for me ! Thank you !
Super interesting. Can’t wait for next part. Really exited about the solution for collections...
same here
Thank you very much
I have been struggling with this for a while.
Really thank you 🙏
Great video!
Thank you so much!
This is awsome 😍😍😍. I have been loooking for pattern like this for weeks. You are a true savior ❤️. You can explain all laravel advanced concepts as clear as diamonds... thank you 👏👏
I wonder if I can use this pattern for grandchildren structure.
Imagine there is a Estate Agency Business and Business logic is like this,
Property Development Site (Grandparent) > Building (Parent) > Property (Child)
Currenly I solved this with single Property model but I am sure there is better way.
i want to stick to vanilla laravel without using a plugin and have a problem with single-table-inheritance (STI):
if i have another class (i.e. "Alert") that is referencing a User via:
public function user() {
return $this->belongsTo(User::class);
}
And on the User-Class i have:
public function alerts() {
return $this->hasMany(Alert::class);
}
This will not work with STI.
If i would call \App\Models\Doctor::all()->first()->alerts an SQL Exception will be fired, telling me, that an unknown column (alerts.doctor_id) is used.
How do i prevent Laravel from using the wrong foreign-id-field? In this case the field alerts.user_id should be used, as it is derived from the same table.
Thanks in advance!
Hey! If you don't want to use a package you'll have to write your own implementation. I suggest you take a look at Caleb's parental package and how it deals with those issues.
how do you deal with models that require additional data? e.g. doctor's specialization qualifications that the nurses don't have?
You can use the delegated type pattern. Check out my video "Building a timeline like Facebook's"
@ but that will be the opposite of what is shown here.. i would like to stick to single-table as doctors and nurses share quite a number of common fields
@@SEOng-gs7lj you can add nullable columns and determine which models will use them
@ haha okayyy.. i was hoping there was an even cooler way to do it thanks!
@@SEOng-gs7lj I'm kind of no-useless-nullables guy. In that case, I would use Multi-Table Inheritance instead of STI. Probably what he meant as "delegated type pattern". There is no other way around it. You either have 1 giant table with lots of nullables, or create multiple tables.