Laravel hasManyThrough,with属于其他两个模型之间

Laravel hasManyThrough, with belongs to between the other two models

不清楚题名是否清楚,我会尽量详细说明:

表格:

维修(型号:维修):

id
id_supplier
id_store

供应商(型号:供应商):

id

商店(型号:商店):

id

关系:

修复:

public function supplier() {
    return $this->belongsTo('App\Supplier', 'id_supplier');
}

供应商:

public function repairs() {
    return $this->hasMany('App\Repair', 'id_supplier');
}

商店:

public function repairs() {
    return $this->hasMany('App\Repair', 'id_store');
}

我在这里需要的是收集与给定商店合作的所有供应商,通过在那里进行的维修,所以我首先想到的是 hasManyThrough,但不知道该传递什么作为 4º 参数:

商店:

public function suppliers() {
    return $this->hasManyThrough('App\Supplier', 'App\Repair', 'id_supplier', '<NO IDEIA WHAT'S HERE>');
}

我错过了什么?这是正确的方法吗?

我认为你不能在这里使用 HasManyThrough。在您的代码中尝试这样的事情:

$store = Store::with('repairs.supplier')->find($id_store);//eager load
$repairs = $store->repairs->pluck('supplier');//get array of suppliers id
$suppliers = (new Collection($repairs))->unique();//get unique collection of suppliers (without duplicates)

其中 $id_store 是您要获取的供应商的商店 ID