如何获得DB插入Laravel的反馈

时间:2018-09-28 10:44:57

标签: php laravel laravel-5

我的数据库插入查询如下

DB::table('job_details')->insert([
    'job_id'        => $jobId,
    'item_id'       => $itemId,
    'type_id'       => $typeId,
    'qty'           => $qnty,
    'laminating'    => $laminating,
    'mat_id'        => $matId,
    'rates'         => $rates,
    'sqft'          => $sqft,
    'ups'           => $ups,
    'master_qty'    => $masterQnty
]);

,如果查询成功或失败,我想获取状态。

2 个答案:

答案 0 :(得分:1)

插入method返回一个boolean,您可以将结果保存在变量中,并检查结果是否为真。

$queryState = DB::table('job_details')->insert([...])
if($queryState) {
    // the query succeed
} else {
    // the query failed
}

答案 1 :(得分:1)

在laravel中执行数据库操作时,该方法还将返回true或false的响应以捕获异常,您可以将代码保留在try catch块中。

 try{
         $response= DB::table('job_details')->insert([
            'job_id'        => $jobId,
            'item_id'       => $itemId,
            'type_id'       => $typeId,
            'qty'           => $qnty,
            'laminating'    => $laminating,
            'mat_id'        => $matId,
            'rates'         => $rates,
            'sqft'          => $sqft,
            'ups'           => $ups,
            'master_qty'    => $masterQnty
        ]);
         if($response)
            echo 'Query was successfull';
        else
            echo 'There was some error';
    }catch{
        print_r($e->getMessage);
    }