Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Add a method in query builders that will substitue question mark with the bindings value #901

Closed
ajcastro opened this issue Nov 23, 2017 · 1 comment

Comments

@ajcastro
Copy link

ajcastro commented Nov 23, 2017

I think it is helpful if there is a method that will substitue question mark with the bindings value for debugging queries.. so you can copy-paste it in your sql editor. This is very useful for debugging long complex queries.

How about sql()?

User::where('is_active, 1)->sql(); 
// this will print 
// select * from `users` where `is_active` = 1
// instead of
// select * from `users` where `is_active` = ?
if (!function_exists('sql')) {
    function sql($query)
    {
        $sql = $query->toSql();

        foreach ($query->getBindings() as $binding) {
            $sql = str_replace_first('?', "'{$binding}'", $sql);
        }

        return $sql;
    }
}

if (!function_exists('str_replace_first')) {
    function str_replace_first($from, $to, $subject)
    {
        $from = '/'.preg_quote($from, '/').'/';

        return preg_replace($from, $to, $subject, 1);
    }
}
@ntzm
Copy link

ntzm commented Nov 23, 2017

I believe this has been PRed and rejected in the past:

laravel/framework#21796

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants