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

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

php larvel 加入所有表時刪除多個表中的數據

php larvel 加入所有表時刪除多個表中的數據

PHP
元芳怎么了 2021-12-03 19:03:08
我的代碼有問題。我必須使用 id 從 5 個表中刪除數據,這些表由主鍵和外鍵連接。這是我嘗試過的,但它會顯示添加訂閱表 ID。但是已經添加了訂閱表 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貢獻1820條經驗 獲得超10個贊

您可以暫時禁用外鍵約束


在 Laravel 中,您可以執行以下操作:


$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)));


查看完整回答
反對 回復 2021-12-03
?
RISEBY

TA貢獻1856條經驗 獲得超5個贊

$orderId 不是關聯數組,因此出現錯誤。試試這個代碼。


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)));


查看完整回答
反對 回復 2021-12-03
  • 2 回答
  • 0 關注
  • 287 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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