douzong3599 2017-04-09 22:27
浏览 104
已采纳

尝试/捕捉不使用laravel 5.4

I know there are a lot of answers there about this question but none of them really helped me.

// update name

Route::put('/profile/update', function(Request $request){
$name = $request->input('name');

   try{
         echo DB::table('users')->where('id',Auth::id())->update(['name' => $name]);
      }
   catch(\Exception $e){
     // do task when error
      echo $e->get_message();
   }
});

I have also tried delete method but that is also not working can you please figure out what is going on. thanks.

  • 写回答

3条回答 默认 最新

  • douluozhan4370 2017-04-09 23:56
    关注

    from your code it seems like the code will never hit the catch which is nothing to do with laravel actually. your issue is a SQL one.

    you're trying to update a record and updating a none-existing row will never fail in SQL. so I suggest to handle the case manually by checking the result value and replacing the try and catch with if else

    BTW @Learner is 100% right about get_message() it's not in laravel as I know replace it in the future with getMessage()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部