gpt4 book ai didi

php - 脱离深层关系(关系关系)的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-04 08:12:16 25 4
gpt4 key购买 nike

我有一个属于多个功能的产品属于多个变体。
产品

public function features()
{
return $this->belongsToMany(Feature::class, 'product_feature', 'product_id', 'feature_id');
}
特征
public function variants()
{
return $this->belongsToMany(Variant::class, 'feature_variant', 'feature_id', 'variant_id');
}
这里 变体 关系取决于 功能 关系。如果 功能 删除了相关的 变体 关系也应该删除。最好的方法是什么?
我试过这个
$product->features()->each(function($feature){
$feature->variants()->each(function($variant){
$variant->sync([]); // $variant()->sync([]) returns Function name must be a string in...
});
});
但得到 BadMethodCallException with message 'Call to undefined method App\Models\ProductFeatureVariant::sync()'这里的正确方法是什么?

最佳答案

我认为最好的 id 是使用 cascade它会自动成为你想要的

$table->foreign('feature')->references('id')->on('variant')->onDelete('cascade');
您可以阅读更多关于 cascade 的信息在此 link

关于php - 脱离深层关系(关系关系)的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65896662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com