QueryException.php update formatMessage to use getRawSql method#57958
QueryException.php update formatMessage to use getRawSql method#57958akaDJon wants to merge 2 commits intolaravel:12.xfrom
Conversation
wrong sql string fix
|
Tests failing. |
Very strange errors: Was It my fault? Ok, I'll see |
|
Ok, I understand. The issue happens because $this->toRawSql() uses the DB facade. |
|
I’d like to propose an alternative solution. Currently, QueryException does not receive the Connection instance, which makes it impossible to reliably generate the SQL (similar to toRawSql()) inside the exception without relying on facades. This leads to issues in the test environment — specifically the "A facade root has not been set" error. A cleaner and more robust approach would be to modify the QueryException constructor so that it accepts the Connection object. This would allow the exception to access the grammar and bindings directly, without using facades and without requiring the application container to be bootstrapped. I’d like to ask whether the team is interested in this approach. If so, I can update the implementation accordingly. |
Fix: Improve SQL query rendering in QueryException
Problem
This Pull Request addresses an issue where the SQL query displayed in a database exception message (e.g., in
QueryException::formatMessage) was rendered incorrectly, specifically within theVALUESclause ofINSERTstatements.When binding parameters if a value was empty or NULL, the formatting logic sometimes failed to explicitly render the corresponding value or placeholder, resulting in two commas appearing sequentially in the displayed SQL:
..., value1, , value3, ....This faulty rendering led developers to believe there was a syntax error in the generated SQL (an incorrect number of parameters/values), when the actual issue was only in the display logic of the exception handler.
The incorrect formatting masked the true structure of the query and the bound parameters, hindering debugging efforts.
Example of Incorrect Rendering:
Example of Сorrect Rendering: