Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stemie's avatar

Get error message from raw database query DB::getPdo()

How can I get the errror message from the raw query, for example: DB::connection('mysql2')->getPdo()->exec($sql); this just returns 1 or 0

I need to know if the query was unsuccessful because of an error or if the data was unchanged, I'm running an UPDATE query.

PDO has errorInfo() in php but unsure how to access this in laravel.

I can't use eloquent, it needs to be raw. I'm open to suggestions though perhaps my approach is wrong.

0 likes
1 reply
Talinon's avatar

Give this a try:

$mysql2 = DB::connection('mysql2')->getPdo();
$mysql2->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$mysql2->exec($sql);

That should throw an exception when an error occurs

Please or to participate in this conversation.