12

I'm trying to bind an array in a raw WHERE IN query in the Laravel DB

example:

$arr = [1,2,3];
DB::select(DB::raw("select * from test1 WHERE id IN ? "), [$arr]);

for some reason the array is not being changed to the following query:

select * from test1 WHERE id IN (1,2,3)

does someone know if I can do this somehow?

1
  • TRY : DB::select(DB::raw("select * from test1 WHERE id IN ? "), $arr); Commented Aug 16, 2017 at 10:08

6 Answers 6

8
$arr = [1,2,3];
$placeholders = implode(",", array_fill(0, count($arr), '?'));

DB::select("select * from test1 WHERE id IN ($placeholders)", $arr);

This example:

  1. supports any length of an array
  2. does not contain a cycle
  3. passes arguments safely

In this example, I fill a new array with such a number of question marks equal to the array length. Then I glue the new array, separated by commas, and get "?,?,?, ...". Then I insert this substring between the parentheses operator "IN". And I pass the array of items itself as the second parameter of the select function. As a result, each element of the array of elements has its own placeholder in the "IN" operator.

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

4 Comments

While this may answer the question, it would be a good thing to also include an explanation why it solves the issue (how it works), so that any future readers can learn from it.
I agree with the above comment. The code in this answer is good but there's no explanation as to why this works, why you need to do this, and how this answer is better than the other existing answers.
@El_Vanja This example supports any length of an array. Does not contain a cycle. Passes arguments safely. And this example works because I fill a new array with such a number of question marks equal to the number of elements of the argument array. Then I glue the array, separated by commas, and get "?,?,?, ...". Then I insert this substring between the parentheses operator "IN". And I pass the array of items itself as the second parameter of the select function. As a result, each element of the array of elements has its own placeholder in the "IN" operator
@nilecrocodile Yes, I can decipher what's happening due to the experience I have. I wasn't asking an explanation for myself, I wanted to point out that you should always include an explanation in the answer itself. That way, when someone inexperienced reads it, they can at least have some general idea what's happening here. I would suggest you edit the answer and include the contents of your comment in there. Some people only glance over comments and may miss additional information that was provided there.
5

try it in laravel:

$arr = [1,2,3];
$result = DB::table('test1')->whereIn('id', $arr)->get();
dd($result);

And use this one for your raw query:

$arr = [1,2,3];
$arr = join(",",$arr);
$result =  DB::select(DB::raw("SELECT * FROM test1 WHERE id IN (".$arr.")"));
dd($result);

For preventing sql injection you use something like which i have mentioned below.

 $arr = [1,2];
 $arr = join(",",$arr);
 $result =  DB::select(DB::raw("SELECT * FROM test1 WHERE id IN (?,?)"),$arr);
 dd($result);

it will be work for you.

4 Comments

Normally this would work but i have to do this in a raw query because I am using other things in the raw query
i have edited my post for raw query also,check it please
@gauraw You must count $arr to prepare a block of "?". It's not necessary.
@Davuz Its a example only,Sure we can count $arr to prepare a block for that.
1

or

DB::table("test1")->whereIn('id', $arr)->get();

https://laravel.com/docs/5.4/queries#where-clauses

1 Comment

Normally this would work but i have to do this in a raw query because I am using other things in the raw query
0

or Eloquent :

$q= TestModel::where('id',$arr)->get();

2 Comments

in complex query true , but in simple ones why not use eloquent ;)
Yeah i know am talking in general , no problem bro and good luck ^^
0

Try:

$array = [2,5,9,7,5];
$arrPos = [];
$arrBind = [];
foreach ($array as $pos => $id) {
    $arrPos[] = ":id_{$pos}";
    $arrBind["id_{$pos}"] = $id;
}

$sql = 
"  SELECT * FROM test1 WHERE id IN (".implode(', ', $arrPos).") ";
$rs = DB::select($sql, $arrBind);

Comments

0

I slightly optimized @nilecrocodile code so that we don't need to construct array and create string from and it. Instead, we'll create placeholders string this way:

$placeholders = substr(str_repeat(', ?', count($arr)), 2);

DB::select("select * from test1 WHERE id IN ($placeholders)", $arr);

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.