如何从具有许多不同关系的表中删除并删除项目和所有相关?

我正在尝试删除具有 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)


慕容森
浏览 195回答 2
2回答

LEATH

试试这个:$dbProduct->delete();Brand::where('product_id', $dbProduct->id)->delete();Tags::where('product_id', $dbProduct->id)->delete();并添加所有表

吃鸡游戏

这是我到达的解决方案:ini_set('max_execution_time', 300);        $products = $request->all();        try {            DB::beginTransaction();            foreach ($products as $product) {                $product = $this->getProduct($product['SKU']);                $product->sizes()->detach();                $product->tags()->detach();                $product->fields()->detach();                $product->countries()->detach();                $product->exportationFactors()->delete();                $product->delete();                DB::commit();            }            DB::commit();        } catch (Exception $e) {            DB::rollBack();            throw new HttpException(500, 'Sucedio un error eliminando la información favor intentar de nuevo');        }在 vue.js 中sendData(data, index) {                this.loading = true;                this.error = {};                this.$http.post(this.baseUrl, data, this.params)                    .then(                        () => {                            this.successAction();                            this.statuses[index] = 1;                            this.errorDis = false;                        },                        (res) => {                            this.showErrors(res);                            this.statuses[index] = 2;                            this.errorDis = true;                        }                    );            },
打开App,查看更多内容
随时随地看视频慕课网APP