2

I am trying to diedump the query on my index screen using this line of code:

dd(DB::table('members')->where('name', '=', 'Tycho')->toSql());

Now the problem is that when I am displaying the query on my screen I get this:

"select * from `members` where `name` = ?"

My final goal of these lines of code is that I can save offline queries and execute them when the application is online. Unless someone has a solution for this, I'll have to save the queries in a database.

3 Answers 3

3

You are seeing the ? placeholders as Laravel uses Prepared Statements.

See Ijas Ameenudeen's answer on another SO question which details how to add a toRawSql() macro on the Eloquent builder which will replace the placeholders with the bindings that you supplied to the original query.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try this:

    DB::enableQueryLog();
    DB::table('members')->where('name', '=', 'Tycho')->get();
    echo "<pre>";
    print_r(DB::getQueryLog());

Comments

0

This is because you are using the toSql method, you can use the getBindings method to get the values / bindings.

oneliner:

$query = DB::table('members')->where('name', '=', 'Tycho')->toSql();
// will give the raw query with bindings.
$sqlWithBindings = str_replace_array('?', $query->getBindings(), $query->toSql());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.