简体   繁体   English

Laravel Eloquent 模型间多对多的多重关系

[英]Laravel Eloquent multiple relations of many-to-many between models

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:我的困难来自于以下几点:

  1. The Clinic is in many World Regions/Continents;诊所遍布世界许多地区/大陆;
  2. It is also, in many Countries of each Region它也是,在每个地区的许多国家
  3. It is also in may Cities of each Country;它也在每个国家的5个城市;
  4. 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).我的第一种方法太笨拙,肯定行不通,因为我试图构建一个包含两个以上 ID 的数据透视表,而另一种方法是在一个表中进行(我认为这是绝对不正确的)。

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.我想在发布 json 响应时通过对数据透视表使用sync()方法来添加/更新/删除。 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.在此先感谢您对如何最好地设置这一点的任何见解,因为实际上我对这种高级级别的 Eloquent 关系还很陌生。

Following Jonas Staudenmeir suggestion and help, I ended up solving the problem with one simple pivot table and the respective methods for the relationship.遵循 Jonas Staudenmeir 的建议和帮助,我最终用一个简单的数据透视表和相应的关系方法解决了这个问题。 Thus the following, in case someone has the same issue:因此,如果有人遇到同样的问题,请执行以下操作:

Created a migration for the pivot table为数据透视表创建迁移

public function up()
{
    Schema::create('address_area', function (Blueprint $table) {
            $table->integer('address_id')->unsigned()->index();
            $table->integer('area_id')->unsigned()->index();
            $table->primary(['address_id', 'area_id']);
    });
}

Now, on the App\\Models\\Address::class added the method for the relationship:现在,在App\\Models\\Address::class添加了关系的方法:

/**
 * Method to establish the relationship between the address and the practice areas
 *
 * @access public
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 * @since  1.0.1
 */
public function areas()
{
    return $this->belongsToMany(PracticeArea::class);
}

On the other end, the App\\Models\\PracticeArea::class added the method for the relationship:在另一端, App\\Models\\PracticeArea::class添加了关系的方法:

/**
 * Method to establish the relationship between the practice areas and the address
 *
 * @access public
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 * @since  1.0.1
 */
public function address()
{
    return $this->belongsToMany(Address::class);
}

Now, every time it's added or removed a practice area on the clinic branch, which has the city_id , country_id and region_id columns, the pivot table is then synced:现在,每次在诊所分支上添加或删除具有city_idcountry_idregion_id列的实践区域时, city_id同步数据透视表:

// synchronize (add/delete) entries on the pivot table
// $practices array of practice areas
$address->areas()->sync($practices);

This way, multiple queries can be done on both sides – Clinic branches or Practice areas by city, country or region.通过这种方式,可以在双方进行多个查询 - 诊所分支机构或按城市、国家或地区划分的实践区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM