为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

萧十郎 2022-09-03 17:35:56
我正在尝试在获取数据上实现循环,但它给出了属性不存在的错误。$SaleOrderProducts = SaleOrderProducts::where('sale_order_id', $id)->get()->groupBy('purchase_order_product.products.id');我试过这个foreach($SaleOrderProducts as $product){  return $product->products->id;}当我刚回来的时候$productsforeach($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"}}}]
查看完整描述

2 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞


试试下面的代码


foreach($SaleOrderProducts as $product)

{

  return current($product)->purchase_order_product->products->id;

}


查看完整回答
反对 回复 2022-09-03
?
MMTTMM

TA贡献1869条经验 获得超4个赞

具有给定 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);


查看完整回答
反对 回复 2022-09-03
  • 2 回答
  • 0 关注
  • 134 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信