MrChrissss MrChrissss -3 years ago 251
PHP Question

WHERE IN array binding in DB::raw laravel 5.4

I'm trying to bind a 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?

Answer Source

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);

it will be work for you.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download