为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕容森 2022-01-02 19:31:52
我正在尝试删除具有 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)
查看完整描述

2 回答

?
LEATH

TA贡献1936条经验 获得超7个赞

试试这个:


$dbProduct->delete();

Brand::where('product_id', $dbProduct->id)->delete();

Tags::where('product_id', $dbProduct->id)->delete();

并添加所有表


查看完整回答
反对 回复 2022-01-02
?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

这是我到达的解决方案:


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;

                        }

                    );

            },


查看完整回答
反对 回复 2022-01-02
  • 2 回答
  • 0 关注
  • 194 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号