繁体   English   中英

加入所有表时删除多个表中的数据php larvel

[英]delete data from multiple tables when joined all tables php larvel

我的代码有问题。 我必须使用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": "->"
        },

您可以暂时禁用外键约束

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM