亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從具有許多不同關系的表中刪除并刪除項目和所有相關?

如何從具有許多不同關系的表中刪除并刪除項目和所有相關?

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 關注
  • 181 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號