laravel使用查询构建器

时间:2018-02-21 10:54:55

标签: php laravel laravel-5.5 laravel-query-builder

我如何在我的laravel应用程序代码中使用查询构建器了解更新的更改,如下所示  公共功能更新(请求$请求,公告$公告)     {

     $announcement_update = DB::table('announcement')
        ->where('id', $request->announcement_id)
        ->update($announcement_data);

    if($announcement_update){
        return redirect('announcement')->with('success','Announcement updated               successfully');
    }
    elseif(need conditionfor this){
        return redirect('editAnnouncement/'.encrypt($request->announcement_id))
        ->with('error','NO changes have been made in the form fields');
    }
    else{
        return redirect('editAnnouncement/'.encrypt($request->announcement_id))
        ->with('error','There seems some error in updating announcement. Please try again');
    }
}

2 个答案:

答案 0 :(得分:0)

更新查询将返回受影响的行数。您可以使用\Illuminate\Database\QueryException例外捕获异常或查询错误。

try {
    $announcement_update = DB::table('announcement')
        ->where('id', $request->announcement_id)
        ->update($announcement_data);

    if($announcement_update > 0){
        return redirect('announcement')->with('success','Announcement updated               successfully');
    }
    else
    {
        return redirect('editAnnouncement/'.encrypt($request->announcement_id))
        ->with('error','NO changes have been made in the form fields');
    }

} catch (\Illuminate\Database\QueryException $ex) {
    return redirect('editAnnouncement/'.encrypt($request->announcement_id))
    ->with('error','There seems some error in updating announcement. Please try again');
}

答案 1 :(得分:0)

您可以编写一个事件并在更新时触发它,并存储更改