The simplest way to catch any sql
syntax or query errors is to catch an IlluminateDatabaseQueryException
after providing closure to your query:
try {
$results = DB::connection("example")
->select(DB::raw("SELECT * FROM unknown_table"))
->first();
// Closures include ->first(), ->get(), ->pluck(), etc.
} catch(IlluminateDatabaseQueryException $ex){
dd($ex->getMessage());
// Note any method of class PDOException can be called on $ex.
}
If there are any errors, the program will die(var_dump(...))
whatever it needs to.
Note: For namespacing, you need to first
if the class is not included as a use
statement.
Also for reference:
Laravel 5.5 API - Query Exception
Laravel 8.x API - Query Exception
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…