在DB :: raw laravel 5.4中的数组绑定中 [英] WHERE IN array binding in DB::raw laravel 5.4
本文介绍了在DB :: raw laravel 5.4中的数组绑定中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Laravel DB
I'm trying to bind an array in a raw WHERE IN
query in the Laravel DB
示例:
$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?
推荐答案
在laravel中尝试:
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);
为防止sql注入,您可以使用下面提到的类似内容.
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);
它将为您服务.
这篇关于在DB :: raw laravel 5.4中的数组绑定中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文