Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果分销商和产品模型与Distributorproduct模型直接相连,我如何在我的三个模型之间建立关系_Php_Laravel_Laravel 6_Eloquent Relationship - Fatal编程技术网

Php 如果分销商和产品模型与Distributorproduct模型直接相连,我如何在我的三个模型之间建立关系

Php 如果分销商和产品模型与Distributorproduct模型直接相连,我如何在我的三个模型之间建立关系,php,laravel,laravel-6,eloquent-relationship,Php,Laravel,Laravel 6,Eloquent Relationship,分发服务器表-id 产品表-id DistributorProduct表-分销商id、产品id 分销商模型- public function product() { return $this->hasMany(Distributorproduct::class); } 产品模型- public function distributor() { return $this->hasMany(Distributorproduct::class); } 分销商产品模型- public f

分发服务器表-id 产品表-id DistributorProduct表-分销商id、产品id

分销商模型-

public function product() { return $this->hasMany(Distributorproduct::class); }
产品模型-

public function distributor() { return $this->hasMany(Distributorproduct::class); }
分销商产品模型-

public function distributor() { return $this->belongsTo(Distributor::class); }

public function product() { return $this->belongsTo(Product::class); }
如果我写
$product->distributor
,它会给我distributorproduct的所有细节,但我需要的是distributorproduct的细节,而不是distributorproduct

如果我写
$distributor->product
,它会给我distributorproduct的所有详细信息,但我需要的是产品的详细信息,而不是distributorproduct


提前感谢…

我不清楚产品、分销商产品和分销商之间的关系,但我认为您需要指定分销商和产品(以及产品->分销商)之间不同类型的关系。 在Distributor modal类中,您可以尝试如下定义产品方法:

public function product(){
返回$this->hasOneThrough(Product::class,Distributorproduct::class);
}
以及Product modal类中的distributor方法,如下所示:

公共函数分发服务器(){
返回$this->hasOneThrough(Distributor::class,Distributorproduct::class);
}
但是你的里程数可能会有所不同,只有当一个产品有一个分销商(可能),而分销商有一个产品(我想不太可能)时,这种关系才行


Laravel文档有一个非常好的文档。特别关注有一个到的段落,有许多到的段落。

有一个到是一个单向关系,如果我有表关系,如distributor->distributorproduct(外键distributor\u id)->product(外键distributorproduct\u id)但在我的例子中,我无法将distributorproduct_id的主键存储在product表中,因此如果use hasOneThrough生成sql查询错误。。。。