/** * Attach recipe for the last entry if the date is today * @param $date * @param Entry $entry * @param $index */ private function attachRecipe($date, Entry $entry, $index) { if ($date === Carbon::today()->format('Y-m-d') && $index === 1) { $recipe_ids = Recipe::where('user_id', $this->user->id)->lists('id')->all(); if ($recipe_ids) { $entry->recipe()->associate(Recipe::find($recipe_ids[0])); } } }
/** * 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); }