php - 连接所有表时删除多个表中的数据 php laravel

标签 php mysql laravel

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

最佳答案

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

关于php - 连接所有表时删除多个表中的数据 php laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57636971/

相关文章:

php - Doctrine2 QueryBuilder NOT IN 语句不起作用

mysql - 使用 VB.NET 通过 SSL 远程访问 MySQL

mysql BLOB问题

php - 使用 pre_get_posts 构建自定义查询不起作用

php - 如何使用 Laravel + Jquery Ajax 从文本区域获取值到文本区域?

php - 在 laravel 中存储来自队列的已处理作业

php - MySQL - 如何根据另一个表中的列进行排序

javascript - Laravel 5.3 上带有 javascript x-editable 的 TokenMismatchException

mysql - 删除具有关系的相关列的最佳方法

php - 更改 Magento 主题的页脚