I would like to have some feedback on how to join two tables using Laravel. I have read the documentation but still I am a little bit confused. And I'm still thinking on how to handle the problem. See... I have two tables one for the kids information and the other for the vaccines, I decided to put it apart because there are a lot of vaccines and I don't want the user to input every vaccine each time they add a new kid to the system. I am planning on making some type of checkboxes for the administrator to select the vaccines each kid has, but then I have the date, so I don't know what will be the best approach. And one of my questions is, how will I retrieve the data from the vaccines table when the user is filling out the kids information? Or do you guys have a better sugestion? I already have the table of the kids up and running, but now I have to add the vaccines. I would glady appreaciate your help. I will also leave a picture of the two tables.
To my understanding you will need to keep another table that would manage the interaction between kids
and vaccines
lets say kids_vaccines
.
This means that, every kid that receive one or many vaccines would have their record added with timestamp of the time the vaccine(s) was received
An example would be:
Having KidsVaccine
model with relationship with Kid
and Vaccine
like
public function kid()
{
return $this->belongsTo(Kid::class, 'idkid');
}
public function vaccine()
{
return $this->belongsTo(Vaccine::class, 'idvaccine');
}
And both your Kid
model and Vaccine
model can bear a hasMany()
relationship with KidsVaccine
This means that, in lets say kids_vaccines
table you would be managing idkid
and idvacccine
as both foreign keys (ofcourse your timestamps are here, so as to know when a vaccine was applied to a kid).
This results that you can easily create a kids with vaccines retrievable from the vaccines
table.
If there is anything unclear you can point it.
Hope this helps :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.