使用数组值在laravel中选择数据库表

时间:2017-07-20 08:00:23

标签: arrays laravel laravel-4

我有这个排序数组与前5个产品

foreach ($bestsellers as $item) {
      $data = json_decode($item->order_details, true);
      $product_ids[] = key($data);
}
      arsort($product_ids);
      $three = array_unique(array_slice($product_ids, 0, 5, true));
      print_r($three);

结果是

Array
(
    [0] => 1 
    [1] => 2 
    [2] => 3 
    [3] => 4 
    [4] => 5
)

1,2,3,4,5是产品ID。是否可以在查询到产品表中使用此值,以便我可以在页面上显示有关产品的更多信息。不只是身份证。

这样的东西
Product::where('product_id', $value);

更新

$best = Product::whereIn('product_id', $three)->get();

foreach($best as $info)
{
    dd($info->title);
}

1 个答案:

答案 0 :(得分:0)

是的,你应该使用whereIn作为数组。实际上,whereIn将第二个参数作为指定列的值数组。

Product::whereIn('product_id', $value);

whereIn()

  

whereIn方法验证是否包含给定列的值   在给定的数组中: