/**
  *
  * @param Food $food
  * @param $tempFood
  */
 private function attachUnits(Food $food, $tempFood)
 {
     foreach ($tempFood['units'] as $unitName) {
         $unitId = Unit::where('user_id', $this->user->id)->where('name', $unitName)->first();
         $food->units()->attach($unitId, ['calories' => 5]);
     }
 }
 /**
  * UPDATE /api/foods/{foods}
  * @param Request $request
  * @param Food $food
  * @return Response
  */
 public function update(Request $request, Food $food)
 {
     if ($request->get('updatingCalories')) {
         //We are updating the calories for one of the food's units
         $food->units()->updateExistingPivot($request->get('unit_id'), ['calories' => $request->get('calories')]);
     } else {
         // Create an array with the new fields merged
         $data = array_compare($food->toArray(), $request->only(['name']));
         $food->update($data);
         if ($request->has('default_unit_id')) {
             $food->defaultUnit()->associate(Unit::findOrFail($request->get('default_unit_id')));
             $food->save();
         }
         if ($request->has('unit_ids')) {
             $food->units()->sync($request->get('unit_ids'));
         }
     }
     $food = $this->transform($this->createItem($food, new FoodTransformer()))['data'];
     return response($food, Response::HTTP_OK);
 }
 /**
  * Transform food response
  * @param Food $food
  * @return array
  */
 public function transform(Food $food)
 {
     $array = ['id' => $food->id, 'name' => $food->name, 'path' => $food->path, 'defaultCalories' => $food->getDefaultCalories(), 'unitIds' => $food->units()->lists('unit_id')];
     /**
      * @VP:
      * I'm doing this here so I can include the units from my IngredientTransformer.
      * What's the proper way of doing this?
      * Actually, $array['units'] is an empty array here. Why?
      * It should be populated, and if I dd what includeUnits returns,
      * the data looks correct.
      */
     if (isset($this->params['units'])) {
         $array['units'] = $this->includeUnits($food);
     }
     //        if ($food->default_unit_id) {
     //            $array['defaultUnit'] = [
     //                'id' => $food->defaultUnit->id,
     //                'name' => $food->defaultUnit->name
     //            ];
     //            $array['defaultUnit'] = $food->defaultUnit;
     //            $array['defaultUnit'] = $this->includeDefaultUnit($food);
     //        }
     return $array;
 }