Last active
August 6, 2023 19:15
-
-
Save Rapkalin/a20cde1e32495d8d65d0826c9d35449c to your computer and use it in GitHub Desktop.
Preview SQL query with Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
Function previewSql() | |
{ | |
$users = User::where(‘id’, 0); | |
$users->union(User::where(‘id’, 1)); | |
$users->union(User::where(‘id’, 2)); | |
$users->union(User::where(‘id’, 3)); | |
// Preview sql statement using getBindings() and toSql() builder methods | |
echo(Str::replaceArray(‘?’, $users->getBindings(), $users->toSql())); | |
/** | |
OUTPUT | |
"(select * from `users` where `id` = 0) | |
union (select * from `users` where `id` = 1) | |
union (select * from `users` where `id` = 2) | |
union (select * from `users` where `id` = 3) | |
" | |
**/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment