-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Add method QueryExecuted::toRawSql()
#52192
Conversation
Could you make the test an integration test? |
Please mark as ready for review when you want me to take another look. 👍 |
bd4e0b3
to
57da994
Compare
Done. I could not find a test class that is more fitting for this use case, so I created a new one that just generally tests database functionality. |
This makes it easier to debug the executed queries when using `DB::listen()`. Before, we did something like this and got a result that was hardly readable: ```php DB::listen(function (QueryExecuted $query): void { $sql = str_replace("\n", ' ', $query->sql); $bindings = json_encode($query->bindings); file_put_contents( filename: storage_path('logs/query.log'), data: "SQL: {$sql} ||| Bindings: {$bindings} ||| Time: {$query->time}ms\n", flags: FILE_APPEND, ); }); // SQL: insert into `competence_detail_criteria` (`competence_criteria_id`, `competence_detail_id`, `valid_from`, `valid_to`, `userid`, `first_id`) values (?, ?, ?, ?, ?, ?) ||| Bindings: [3,1,"2024-07-19 10:59:02","9999-12-31 23:55:55",1,0] ||| Time: 0.84ms ``` With this added method, achieving a readable result becomes much simpler: ```php DB::listen(function (QueryExecuted $query): void { file_put_contents( filename: storage_path('logs/query.log'), data: "SQL: {$query->toRawSql()} ||| Time: {$query->time}ms\n", flags: FILE_APPEND, ); }); // SQL: insert into `competence_detail_criteria` (`competence_criteria_id`, `competence_detail_id`, `valid_from`, `valid_to`, `userid`, `first_id`) values (4, 1, '2024-07-19 11:10:29', '9999-12-31 23:55:55', 1, 0) ||| Time: 0.2ms ```
f3c9260
to
ae7247f
Compare
The failing tests seem unrelated to the changes in this pull request, see https://github.com/laravel/framework/actions/runs/10009570585. |
This makes it easier to debug the executed queries when using
DB::listen()
.Before, we did something like this and got a result that was hardly readable:
With this added method, achieving a readable result becomes much simpler: