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.
Laravel mostly uses camelCase for nomenclature so use:
catch(\Exception $e){
// do task when error
echo $e->getMessage();
}
If not then just dump full $e to see it through:
catch(\Exception $e){
// do task when error
dd($e);
}
snake_case nomenclature is only used for table names in Laravel.
in my case, I must use \Throwable instead of \Exception
try {
//code
} catch (\Throwable $e) {
dd($e);
}
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()