嵌套 Eloquent 关系 - HasOneThrough 问题
Nested Eloquent Relation - HasOneThrough Issue
我为 table 设置了以下设置:
Product_variant
Table
Product_variant -> id, name, code, image
Warehouse
table
Warehouse -> id, name, address
Product Variant Stock
table
Product_Variant_stock -> stock_id, warehouse_id, variant_id, stock_qty
现在,当我尝试访问产品变体信息时,我需要获取有关仓库变体存储的信息。
我在 ProductVariation 模型中尝试过的内容:
public function warehouseName()
{
return $this->hasOneThrough(Warehouse::class, ProductVariantStock::class, 'warehouse_id', 'id');
}
以上未按预期工作。感谢任何帮助。
laravel hasOneThrough 是这样工作的
class ModelA extends Model
{
...
public function cModel()
{
return $this->hasOneThrough(
ModelC::class,
ModelB::class,
'model_a_id', // Key on B that relates to A
'model_c_id', // Key on C that relates to B
'a_id', // Key on A that relates to B
'b_id', // Key on B that relates to C
);
}
}
因此您的代码将是
public function warehouseName()
{
return $this->hasOneThrough(Warehouse::class, ProductVariantStock::class, 'variant_id', 'id', 'id', 'warehouse_id');
}
我为 table 设置了以下设置:
Product_variant
Table
Product_variant -> id, name, code, image
Warehouse
table
Warehouse -> id, name, address
Product Variant Stock
table
Product_Variant_stock -> stock_id, warehouse_id, variant_id, stock_qty
现在,当我尝试访问产品变体信息时,我需要获取有关仓库变体存储的信息。
我在 ProductVariation 模型中尝试过的内容:
public function warehouseName()
{
return $this->hasOneThrough(Warehouse::class, ProductVariantStock::class, 'warehouse_id', 'id');
}
以上未按预期工作。感谢任何帮助。
laravel hasOneThrough 是这样工作的
class ModelA extends Model
{
...
public function cModel()
{
return $this->hasOneThrough(
ModelC::class,
ModelB::class,
'model_a_id', // Key on B that relates to A
'model_c_id', // Key on C that relates to B
'a_id', // Key on A that relates to B
'b_id', // Key on B that relates to C
);
}
}
因此您的代码将是
public function warehouseName()
{
return $this->hasOneThrough(Warehouse::class, ProductVariantStock::class, 'variant_id', 'id', 'id', 'warehouse_id');
}