代码之家  ›  专栏  ›  技术社区  ›  parastoo

更改集合中的数据不会保存

  •  0
  • parastoo  · 技术社区  · 7 年前

    我尝试在这个集合中添加食物属性:

       $days = $days->each(function($day) use($orderedDays, $order) {
    
          $foods = collect($day->foods)->map(function(&$food, $keys) use($day, $orderedDays, $order){
    
            if(in_array($day['id'], $orderedDays)) {
    
              $item = OrderItem::where('order_id', $order->id)->where('meal_plan_day_id', $day['id'])->first();
    
              $qty = $item->qty;
              $food->qty = $qty;
              return $food;
    
            } else {
              $food->qty = 0;
              return $food;
            }
    
          });
    
        });
    

    我的问题是,当我在地图中转储时,$food->数量没有保存:

                        "food_name": "test",
                        "final_price": 51000,
                        "max_no_order": 1,
                        "extra_no_price": 51000,
                        "qty" :2
    

    但每一次都没有挽救

    2 回复  |  直到 7 年前
        1
  •  1
  •   Jerodev    7 年前

    你必须在上使用map函数 $days $天 变量。

    $days = $days->map(function ($day) use ($orderedDays, $order) {
    
        $day->foods = collect($day->foods)->map(function ($food, $keys) use ($day, $orderedDays, $order) {
    
            if (in_array($day['id'], $orderedDays)) 
            {
                $item = OrderItem::where('order_id', $order->id)->where('meal_plan_day_id', $day['id'])->first();
    
                $qty = $item->qty;
                $food->qty = $qty;
            } 
            else
            {
                $food->qty = 0;
            }
    
            return $food;
    
        });
    
        return $day;
    
    });
    
        2
  •  0
  •   Volkan Yılmaz    7 年前

    $days = $days->each(function($day) use($orderedDays, $order) {
    
      $foods = collect($day->foods)->map(function(&$food, $keys) use($day, $orderedDays, $order){
    
        if(in_array($day['id'], $orderedDays)) {
    
          $item = OrderItem::where('order_id', $order->id)->where('meal_plan_day_id', $day['id'])->first();
    
          $qty = $item->qty;
          $food->qty = $qty;
          return $food;
    
        } else {
          $food->qty = 0;
          return $food;
        }
    
      });
    
    });