Laravel 4 Eloquent ORM选择where - 数组作为参数

38

在Eloquent ORM中有解决方案吗?

我有一个包含父级标识的数组:

Array ( [0] => 87,  [1] => 65, ... )

我想选择表PRODUCTS,其中parent_id列等于数组中的任何一个id

2个回答

80

流利:

DB::table('PRODUCTS')->whereIn('parent_id', $parent_ids)->get();

Eloquent:

Product::whereIn('parent_id', $parent_ids)->get();

2
FWIW,在Eloquent示例中,您还需要get()结果:Product::whereIn('parent_id', $parent_ids)->get(); - Ludder

24

你的数组必须像这样:

$array = array(87, 65, "etc");
Product::whereIn('parent_id', $array)->get();
或者
Product::whereIn('parent_id', array(87, 65, "etc"))->get();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接