第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

php larvel 加入所有表時(shí)刪除多個(gè)表中的數(shù)據(jù)

php larvel 加入所有表時(shí)刪除多個(gè)表中的數(shù)據(jù)

PHP
元芳怎么了 2021-12-03 19:03:08
我的代碼有問題。我必須使用 id 從 5 個(gè)表中刪除數(shù)據(jù),這些表由主鍵和外鍵連接。這是我嘗試過的,但它會(huì)顯示添加訂閱表 ID。但是已經(jīng)添加了訂閱表 id。$subscription = DB::table('tbl_asset_subscription')->where('id',$post['asset_id'])->get();        foreach($subscription as $row)        {        DB::table('tbl_asset_subscription')->where('id',$row->id)->delete();        }        $orderId = array();        foreach($subscription as $row)        {        $order = DB::table('tbl_asset_order')->where('subscription_id',$row->id)->first();        $orderId[] = $order->id;        }        foreach($orderId as $row)        {        DB::table('tbl_asset_payment')->where('order_id',$row->id)->delete();        DB::table('tbl_asset_order')->where('id',$row->id)->delete();        }        DB::table('tbl_asset_versions')->where('asset_id',$post['asset_id'])->delete();        DB::table('tbl_assets')->where('id',$post['asset_id'])->delete();        // DB::table('tbl_asset_subscription')->where('asset_id',$post['asset_id'])->delete();        echo(json_encode(array("result" => true)));{    "message": "SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`piccoscript`.`tbl_asset_subscription`, CONSTRAINT `fk_tbl_asset_subscription_tbl_assets` FOREIGN KEY (`asset_id`) REFERENCES `tbl_assets` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION) (SQL: delete from `tbl_assets` where `id` = 1)",    "exception": "Illuminate\\Database\\QueryException",    "file": "/var/www/html/piccoscript/vendor/laravel/framework/src/Illuminate/Database/Connection.php",    "line": 664,    "trace": [        {            "file": "/var/www/html/piccoscript/vendor/laravel/framework/src/Illuminate/Database/Connection.php",            "line": 624,            "function": "runQueryCallback",            "class": "Illuminate\\Database\\Connection",            "type": "->"        },
查看完整描述

2 回答

?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊

您可以暫時(shí)禁用外鍵約束


在 Laravel 中,您可以執(zhí)行以下操作:


$orderId = array();

$subscription = DB::table('tbl_asset_subscription')->where('id',$post['asset_id'])->get(); 

DB::transaction(function() {


    DB::statement('SET FOREIGN_KEY_CHECKS=0');

    foreach($subscription as $row) {

        DB::table('tbl_asset_subscription')->where('id', $row->id)->delete();

    }


    foreach($subscription as $row) {

        $order = DB::table('tbl_asset_order')->where('subscription_id', $row->id)->first();

        $orderId[] = $order->id;


    }



    if($orderId) {

        DB::table('tbl_asset_payment')->whereIn('order_id', $orderId)->delete();

        DB::table('tbl_asset_order')->whereIn('id', $orderId )->delete();

    }


    DB::table('tbl_asset_versions')->where('asset_id', $post['asset_id'])->delete();

    DB::table('tbl_assets')->where('id', $post['asset_id'])->delete();

    DB::statement('SET FOREIGN_KEY_CHECKS=1');


});

echo(json_encode(array("result" => true)));


查看完整回答
反對(duì) 回復(fù) 2021-12-03
?
RISEBY

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊

$orderId 不是關(guān)聯(lián)數(shù)組,因此出現(xiàn)錯(cuò)誤。試試這個(gè)代碼。


foreach($subscription as $row)

{

$order = DB::table('tbl_asset_order')->where('subscription_id',$row->id)->first();

$orderId[] = array(

        'id' => $order->id

    );


}



foreach($orderId as $row)

{

DB::table('tbl_asset_payment')->where('order_id',$row->id)->delete();

DB::table('tbl_asset_order')->where('id',$row->id)->delete();

}



DB::table('tbl_asset_versions')->where('asset_id',$post['asset_id'])->delete();

DB::table('tbl_assets')->where('id',$post['asset_id'])->delete();


// DB::table('tbl_asset_subscription')->where('asset_id',$post['asset_id'])->delete();

echo(json_encode(array("result" => true)));


查看完整回答
反對(duì) 回復(fù) 2021-12-03
  • 2 回答
  • 0 關(guān)注
  • 225 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)