Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add method
QueryExecuted::toRawSql()
(#52192)
* Add method `QueryExecuted::toRawSql()` 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 ``` * Update QueryExecuted.php --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
- Loading branch information