Try this
try { //Your code} catch(\Illuminate\Database\QueryException $ex){ dd($ex->getMessage()); }
OR
use Illuminate\Database\QueryException; try { //Your code } catch(QueryException $ex){ dd($ex->getMessage()); }
My case: add \ before Exception class otherwise it not handle
try{//write your codes here}catch(\Exception $e) { Log::error($e->getMessage()); }
Make sure to use the Exception library in your controller. From there you can do:
try{ Some queries . . .}catch(Exception $e){ return response()->json([ 'error' => 'Cannot excecute query', ],422);}