The long (a little bit weird) way, but works for any Databases
You can use Doctrine's ExceptionConverterInterface
You should install package doctrine/dbal
And find the implementation of this interface by calling
app('db.connection')->getDoctrineConnection()->getDriver()->getExceptionConverter()
or
app(\Illuminate\Database\DatabaseManager::class)->connection()->getDoctrineConnection()->getDriver()->getExceptionConverter()
It applies Doctrine\DBAL\Driver\Exception as a first argument
You can get internal Doctrine\DBAL\Driver\PDO\Exception which implements this interface and instantiate it from your PODException
You can get your PDOException from QueryException this way:
$previous = $queryException->getPrevious();
if ($previous && ($previous instance \PDOException)) {
// ...
}
So, the final solution looks like:
$exceptionConverter = app('db.connection')->getDoctrineConnection()->getDriver()->getExceptionConverter()
$previous = $queryException->getPrevious();
if ($previous && ($previous instance \PDOException)) {
$driverException = $exceptionConverter->convert($previous, null);
if ($driverException instanceof \Doctrine\DBAL\Exception\UniqueConstraintViolationException) {
// Our Exception is about non-unique value
}
}
Please do not use this code in production :)