try catch laravel query code example
Example 1: try and catch laravel
try {
$this->buildXMLHeader();
} catch (\Exception $e) {
return $e->getMessage();
}
Example 2: how to catch query exception in laravel 8
try {
$results = \DB::connection("example")
->select(\DB::raw("SELECT * FROM unknown_table"))
->first();
// Closures include ->first(), ->get(), ->pluck(), etc.
} catch(\Illuminate\Database\QueryException $ex){
dd($ex->getMessage());
// Note any method of class PDOException can be called on $ex.
}