I have a product which belongsToMany features which belongsToMany variants.
Product
public function features()
{
return $this->belongsToMany(Feature::class, 'product_feature', 'product_id', 'feature_id');
}
Feature
public function variants()
{
return $this->belongsToMany(Variant::class, 'feature_variant', 'feature_id', 'variant_id');
}
Here the variant relation depends on feature relation. If the feature relation is removed the related variant relations should be removed as well. What is the best way to do that?
I tried this
$product->features()->each(function($feature){
$feature->variants()->each(function($variant){
$variant->sync([]); // $variant()->sync([]) returns Function name must be a string in...
});
});
but get BadMethodCallException with message 'Call to undefined method AppModelsProductFeatureVariant::sync()'
What is the right approach here?
question from:
https://stackoverflow.com/questions/65896662/what-is-best-way-to-detach-on-a-deep-relationship-relationships-of-relationsip 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…