I have a few models that should relate, in a many-to-many relationship (I presume), for the following:
My Clinic has many practice areas (ex. odontology, psychiatry, general medicine, internal medicine, etc. and operates in several global continents/world regions, in many countries of each region and in many cities of each country. Not all clinic branches have the same Practice Areas.
Where my difficulty comes is because of the following:
- The Clinic is in many World Regions/Continents;
- It is also, in many Countries of each Region
- It is also in may Cities of each Country;
- Each Clinic branch will have it's own Practice Areas;
My first approach was too clunky and would't work for sure, as I tried to build a pivot table with more that two ids and the other was to do it in one table (which I presume is absolutely incorrect).
What would be the best approach to set the database for all these relationships to work together, with the following models I have:
Models\Clinic::class;
Models\PracticeArea::class;
Models\WorldRegion::class;
Models\Country::class;
Models\City::class;
Models\Clinic::class;
public function areas() {
return $this->belongsToMny(PracticeArea::class, 'area_clinic_counrty', 'area_id', 'country_id', 'clinic_id');
}
To simplify, the form has a multiple-select for each region, and each Country (let's discar the Cities here). I would like to add/update/delete by using the sync()
method for the pivots table when the json response is posted. Ex:
data: {
clinic_id: 2,
practices: {
1: ["12","31], // the keys correspond to world region in this ex.
3: ["7", "12", "42"] // the values to practice areas ids
}}
Thanks in advance on any insights on how to best set this, because in fact I'm quite new to the Eloquent relationships at this advanced level.
firm_id
,city_id
,country_id
andregion_id
among the normal address fields - McRuiBelongsToMany
relationship betweenAddress
andPracticeArea
? - Jonas Staudenmeir