コード例 #1
0
 /**
  *
  * @param $date
  */
 private function createEntriesForOneDay($date, $quantity)
 {
     $faker = Faker::create();
     //Create 2 entries for the day
     foreach (range(0, 1) as $index) {
         $entry = new Entry(['date' => $date, 'quantity' => $quantity]);
         $entry->user()->associate($this->user);
         $this->attachFood($entry, $index);
         $this->attachUnit($entry, $index);
         $this->attachRecipe($date, $entry, $index);
         $entry->save();
     }
 }
コード例 #2
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);
 }