使用数组值 select 数据库 table in laravel

Using array values to select database table in laravel

我有这个包含前 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。是否可以在产品 table 的查询中使用此值,以便我可以在页面上显示有关产品的更多信息。不仅仅是身份证。

类似于

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

更新

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

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

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

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

whereIn()

The whereIn method verifies that a given column's value is contained within the given array: