如何在 laravel 中通过分组子句对获取的数据实现循环?

我正在尝试在获取数据上实现循环,但它给出了属性不存在的错误。


$SaleOrderProducts = SaleOrderProducts::where('sale_order_id', $id)->get()->groupBy('purchase_order_product.products.id');

我试过这个


foreach($SaleOrderProducts as $product)


{

  return $product->products->id;

}

当我刚回来的时候$products


foreach($SaleOrderProducts as $product)


{

  return $product;

}

然后它以这种格式返回数据


[{"id":6,"sale_order_id":2,"product_id":5,"purchase_order_product_id":5,"qty_taken":75,"created_at":"2020-01-31 19:04:05","updated_at":"2020-01-31 22:33:41","purchase_order_product":{"id":5,"purchase_order_id":1,"product_id":5,"measurement_unit":"Cartons","expiry_date":"2020-02-01","created_at":"2020-01-31 19:02:03","updated_at":"2020-01-31 22:33:41","status":1,"initial_qty":454,"on_hand_qty":454,"free_qty":389,"orignal_qty":454,"location":null,"products":{"id":5,"name":"Glass","customer_id":1,"product_type_id":1,"product_status_id":1,"code":"d23123","status":1,"description":"kkd","storage_charge_method":"Per Location","volume":4444554,"weight":545,"random_weight":"Yes","warning_threshold_duration":0,"expiry_threshold_duration":0,"low_stock_notification":"off","low_threshold_duration":null,"stock_selection_method":"Locaiton Efficiency Only","strict_stock_selection":"off","stock_sub_selection_method":"Minimise Storage"}}}]



萧十郎
浏览 135回答 2
2回答

HUH函数

试试下面的代码foreach($SaleOrderProducts as $product){  return current($product)->purchase_order_product->products->id;}

MMTTMM

具有给定 json 响应的示例$product = json_decode('[{"id":6,"sale_order_id":2,"product_id":5,"purchase_order_product_id":5,"qty_taken":75,"created_at":"2020-01-31 19:04:05","updated_at":"2020-01-31 22:33:41","purchase_order_product":{"id":5,"purchase_order_id":1,"product_id":5,"measurement_unit":"Cartons","expiry_date":"2020-02-01","created_at":"2020-01-31 19:02:03","updated_at":"2020-01-31 22:33:41","status":1,"initial_qty":454,"on_hand_qty":454,"free_qty":389,"orignal_qty":454,"location":null,"products":{"id":5,"name":"Glass","customer_id":1,"product_type_id":1,"product_status_id":1,"code":"d23123","status":1,"description":"kkd","storage_charge_method":"Per Location","volume":4444554,"weight":545,"random_weight":"Yes","warning_threshold_duration":0,"expiry_threshold_duration":0,"low_stock_notification":"off","low_threshold_duration":null,"stock_selection_method":"Locaiton Efficiency Only","strict_stock_selection":"off","stock_sub_selection_method":"Minimise Storage"}}}]');echo "<pre>";print_r(current($product)->purchase_order_product->products->id);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript