我正在尝试删除具有 6 个关系的产品,如下所示:
品牌->属于(品牌::类);
标签->belongsToMany(Tag::class, 'tags_product');
字段->belongsToMany(Field::class, 'product_field');
尺寸->belongsToMany(Size::class, 'product_size');
国家->belongsToMany(Country::class, 'country_product');
countryProducts->hasMany(CountryProduct::class);
productFields->hasMany(ProductField::class);
exportationFactors->hasMany(ExportationFactor::class);
我是否必须首先摆脱这些关系?
目前我有这个删除功能的代码
ini_set('max_execution_time', 300);
$products = $request->all();
try {
DB::beginTransaction();
foreach ($products as $product) {
$dbProduct = $this->getProduct($product['SKU']);
$dbProduct->brand()->delete();
$dbProduct->tags()->delete();
$dbProduct->fields()->delete();
$dbProduct->sizes()->delete();
$dbProduct->countries()->delete();
$dbProduct->exportationFactors()->delete();
Log::error($dbProduct);
$dbProduct->delete();
}
DB::commit();
} catch (Exception $e) {
DB::rollBack();
throw new HttpException(500, 'Sucedio un error eliminando la información favor intentar de nuevo');
}
我得到这个错误
SQLSTATE[23000]: 完整性约束违规:1451 无法删除或更新父行:外键约束失败 ( sondel. products, CONSTRAINT products_brand_id_foreignFOREIGN KEY ( brand_id) REFERENCES brands( id)) (SQL: delete from brandswhere brands. id= 2)
慕容森
LEATH
吃鸡游戏
随时随地看视频慕课网APP