2

I running this code :

$id = 1;
$email = '[email protected]';

$user = DB::table('users')->where([
  ['id', '=', $id],
  ['email', '=', $email]
])->toSql();
dd($user);

But query builder print is :

select * from `users` where (`id` = ? and `email` = ?)

Why not print is:

select * from `users` where (`id` = 1 and `email` = [email protected])

3 Answers 3

1

the query builder inserts the characters in place of the values to protect you from the sql injections, then he himself will set the values to you as needed, and you will get the finished result, and the fact that you are displayed on the screen is simply viewing the query queries

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

1 Comment

it does not need to print it to you on the screen, it's a special security mechanism, it seems to me that you do not need to see this in the code, it's enough to keep some points in your head, but to display the variables separately, for example, in an array
0

That's how toSql() method works. It just shows you prepared query but doesn't execute it.

To execute the query, do use get(), find(), first() or similar method:

$user = DB::table('users')->where([
    ['id', '=', $id],
    ['email', '=', $email]
])->first();

1 Comment

I know method get() , find(), first(), but my question is query builder not print select * from `users` where (`id` = 1 and `email` = [email protected]) instead of select * from `users` where (`id` = ? and `email` = ?)
0

Query Builder inserts the characters in place of the values to protect you from the SQL Injections.I believe @Дмитрий-Смирнов answered your query well.

Rather then using raw SQL use model directly you may cut-down your line of code using the below code:

$id = 1;
$email = '[email protected]';

$user = User::where('id',$id)
            ->where('email',$email)
            ->get();
dd($user);

Comments

Your Answer

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