Laravel 如何使用Elount解决两个外键和三个表的关系
我无法用雄辩来解决这种关系。我不知道,也许这是非法关系。但我想不出另一种关系。我需要“SaleProduct”行中的“CustomerCodeReferences”行 为什么不:Laravel 如何使用Elount解决两个外键和三个表的关系,laravel,eloquent,Laravel,Eloquent,我无法用雄辩来解决这种关系。我不知道,也许这是非法关系。但我想不出另一种关系。我需要“SaleProduct”行中的“CustomerCodeReferences”行 为什么不: class CustomerCodeReference extends Model { public function saleProducts() { return $this->hasMany(\App\Models\SaleProduct::class, 'product_i
class CustomerCodeReference extends Model
{
public function saleProducts()
{
return $this->hasMany(\App\Models\SaleProduct::class, 'product_id', 'product_id');
}
public function documents()
{
return $this->hasMany(\App\Models\Document::class, 'customer_id', 'customer_id');
}
public function scopeCustomer($query, int $customer)
{
$query->where('customer_id', $customer);
}
}
项目
的客户代码参考
可通过以下方式访问:
$project->customerCodeReference()->customer(1)->get();
我不懂这个图表。您正在将文档中的customer_id与CustomerCodeReferences中的customer_id链接。这些表和customers表之间的逻辑联系不是更紧密吗?customers有许多文档,文档有许多SaleProduct,SaleProduct有一个产品,这就是逻辑。我正在尝试做的产品可以有许多代码的客户。这就是CustomerCodeReferences存在的原因。我需要从SaleProduct模型中获取特殊的客户代码。我希望我能解释一下。这是正确的做法。这种关系是正确的,但我需要SaleProduct模型中的一行customerCodeReference,它与客户和产品有什么关系。因为我要做的是,每个产品都有不同的代码,我添加了一个范围。这符合你的要求吗?这个范围需要客户id,我需要每个SaleProduct行的代码参考。是的,没错。
class SaleProduct extends Model
{
public function customerCodeReferences()
{
return $this->hasMany(\App\Models\CustomerCodeReference::class, 'product_id', 'product_id');
}
public function documents()
{
return $this->belongsTo(\App\Models\Document::class);
}
}
$project->customerCodeReference()->customer(1)->get();