/** * * @param $ingredient * @return \League\Fractal\Resource\Collection */ public function includeFood($ingredient) { /** * @VP: * Including the units here isn't working. */ $food = $this->item(Food::find($ingredient->food_id), new FoodTransformer(['units' => true])); return $food; }
/** * Entry can be either just a food, or part of a recipe. * When part of a recipe, the store method inserts just one food at a time, * so that the store method is RESTful. * So lots of ajax requests will be made to insert * all the entries for a whole recipe. * POST /api/menuEntries * @param Request $request * @return Response */ public function store(Request $request) { $entry = new Entry($request->only(['date', 'quantity'])); $entry->user()->associate(Auth::user()); $entry->food()->associate(Food::find($request->get('food_id'))); if ($request->get('recipe_id')) { $entry->recipe()->associate(Recipe::find($request->get('recipe_id'))); } $entry->unit()->associate(Unit::find($request->get('unit_id'))); $entry->save(); $entry = $this->transform($this->createItem($entry, new MenuEntryTransformer()))['data']; return response($entry, Response::HTTP_CREATED); }
/** * Get recipe contents and steps. * Contents should include the foods that belong to the recipe, * along with the description, quantity, and unit * for the food when used in the recipe (from food_recipe table), * and with the tags for the recipe. * Redoing after refactor. Still need description, quantity, unit. * @param $recipe * @return array */ public function getRecipeInfo($recipe) { $recipe = transform(createItem($recipe, new RecipeWithIngredientsTransformer()))['data']; //For some reason the units for each food aren't being added to the food //from my IngredientTransformer, so add them here foreach ($recipe['ingredients']['data'] as $index => $ingredient) { $units = Food::find($ingredient['food']['data']['id'])->units; $units = transform(createCollection($units, new UnitTransformer())); $recipe['ingredients']['data'][$index]['food']['data']['units'] = $units; } return $recipe; }
/** * * @param Entry $entry * @param $index */ private function attachFood(Entry $entry, $index) { $food_ids = Food::where('user_id', $this->user->id)->lists('id')->all(); $entry->food()->associate(Food::find($food_ids[$index])); }