I'm getting into troubles to capture and retrieve the MySQL Error codes and messages using Laravel 5.2 through the DB Facade with the select method.
What I want to do is to select/call a MySQL function that inserts a simple row in a table with a parameter. However what if the parameter in empty for a column that can't be null? MySQL throws an error (Error Code: 1048
).
The problem is that I'm not getting any error/exception in Laravel in those situations and I can't know if the insert succeeded or not to handle the error in the proper way.
Here is my code:
DB::beginTransaction();
try{
$statement = "select mySQLFunction('".$param."')";
DB::select($statement);
DB::commit();
}catch(\Exception $e){
DB::rollback();
Log::error('Exception: '.$e->getMessage());
}
I wonder if there is a way to capture this kind of error codes and messages so Laravel application can know there was an error in MySQL database. Also to get the return value of the MySQL function in case of success.
Thanks in advance!