Skip to content

Conversation

@mathiasgrimm
Copy link
Contributor

@mathiasgrimm mathiasgrimm commented Jan 9, 2026

Previously on this PR we added the connection details to the QueryException, however, it wasn't getting the correct host for the readPdo.

This PR introduced the latestReadWriteTypeUsed() which allows us to know which connection was last used.

This PR

  • Fixes QueryException showing write connection details when a query fails on the read PDO connection
  • Adds $readPdoConfig property and setReadPdoConfig() method to store read connection configuration
  • Updates getConnectionDetails() to return read config when latestReadWriteTypeUsed() === 'read'

Before

When DB::connection('mysql')->select('...', useReadPdo: true) failed, exception showed:
Host: write.host.example (incorrect)

After

Exception correctly shows:
Host: read.host.example

How to reproduce

Connection Error

  1. Configure a read/write connection with different hosts in config/database.php:
'mysql' => [
  'read' => ['host' => 'invalid.read.host'],
  'write' => ['host' => 'invalid.write.host'],
  'driver' => 'mysql',
  'database' => 'test_db',
  // ...
],
  1. Run a query using the read connection:
DB::connection('mysql')->select('SELECT 1', useReadPdo: true);

Query Error

  1. Configure the read connection with valid hosts/credentials and the write connection with invalid host/credentials in config/database.php:
'mysql' => [
  'read' => ['host' => '127.0.0.1'], // valid
  'write' => ['host' => '127.0.0.2'], // invalid
  'driver' => 'mysql',
  'database' => 'test_db',
  // ...
],
  1. Run a query using the read connection:
DB::connection('mysql')->select('SELECTINVALID', useReadPdo: true);

When using read/write database connections with useReadPdo: true, the
QueryException was always showing the write connection details instead
of the read connection details.

This fix stores the read PDO configuration separately and uses it when
building exception details for queries that failed on the read connection.
@mathiasgrimm mathiasgrimm marked this pull request as draft January 9, 2026 03:46
When using read/write database connections with useReadPdo: true, the
QueryException was always showing the write connection details instead
of the read connection details.

This fix stores the read PDO configuration separately and uses it when
building exception details for queries that failed on the read connection.
When using read/write database connections with useReadPdo: true, the
QueryException was always showing the write connection details instead
of the read connection details.

This fix stores the read PDO configuration separately and uses it when
building exception details for queries that failed on the read connection.
@mathiasgrimm mathiasgrimm marked this pull request as ready for review January 9, 2026 12:30
@taylorotwell taylorotwell merged commit fb944a0 into 12.x Jan 9, 2026
71 checks passed
@taylorotwell taylorotwell deleted the query-exception-followup branch January 9, 2026 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants