Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[12.x] Adds a new method "getRawSql" (with embedded bindings) to the QueryException class #54849

Merged
merged 7 commits into from
Feb 28, 2025

Conversation

erickcomp
Copy link
Contributor

Description

This PR adds a getRawSql to the QueryExceptionClass.

When error reporting/showing tools want to show "compiled" SQL (with embedded bindings), they resort to simple string substitution (which does not covers all cases), using something like Str::replaceArray('?', $bindings, $sql)

This newly introduce getRawSql methods uses the proper grammar of the connection in which the error occurred in order to generate the raw SQL, just like the query builder does.

Usage

->withExceptions(function (Exceptions $exceptions) {
    $exceptions->report(function (QueryException $e) {
        // ...
       MyErrorServiceFacade::report('Error executing SQL: ', $e->getRawSql());
    });
})

This allows one to send the problematic SQL with context, without having to send SQL and bindings separately or even resort to simple string substitution

@taylorotwell taylorotwell merged commit d1b79d7 into laravel:12.x Feb 28, 2025
16 checks passed
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.

2 participants