Skip to content

Instantly share code, notes, and snippets.

@Rapkalin
Last active August 6, 2023 19:15
Show Gist options
  • Save Rapkalin/a20cde1e32495d8d65d0826c9d35449c to your computer and use it in GitHub Desktop.
Save Rapkalin/a20cde1e32495d8d65d0826c9d35449c to your computer and use it in GitHub Desktop.
Preview SQL query with Laravel
<?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