如何在laravel中获取数组内的对象值?

时间:2018-12-15 19:36:08

标签: php laravel laravel-5

我从控制器的视图($ rquest)中收到了此数据表格{} 如何在带有x个项{}的产品数组中获取“ product_id”值以在控制器中使用它们?

form: {
   id: null,
   sale_number: 2,
   client: null,
   products: [
      { product_id:1,description: null, quantity:1, price: 1}
      { product_id:2,description: null, quantity:1, price: 1}
      { product_id:3,description: null, quantity:1, price: 1}
      .....etc
   ],
}

1 个答案:

答案 0 :(得分:1)

您可以使用$request->productsforeach

foreach($request->products as $product)
{
    $product['product_id']
}

如果要将它们保存在数组中,请执行此操作

$product_id_array = array();
foreach($request->products as $product)
{
    product_id_array[] = $product['product_id'];
}