Пример #1
0
 /**
  * Run the database seeds.
  *
  * @return void
  */
 public function run()
 {
     factory(User::class, 50)->create()->each(function ($user) {
         // store 10 rounds per user
         for ($i = 0; $i < 10; $i++) {
             // pick a random course to play on
             $course = Course::orderByRaw("random()")->first();
             // pick a random tee set on that course
             $teeSet = TeeSet::orderByRaw("random()")->where('course_id', $course->id)->first();
             $holes = $course->holes;
             $scores = [];
             // each round has 18 scores
             for ($j = 0; $j < 18; $j++) {
                 $scores[] = factory(Score::class)->make(['hole_id' => $holes[$j]->id]);
             }
             $round = factory(Round::class)->make(['tee_set_id' => $teeSet->id]);
             $round = $user->rounds()->save($round);
             $round->scores()->saveMany($scores);
         }
         $followed = [$user->id];
         // follow 10 random users
         for ($i = 0; $i < 10; $i++) {
             // pick a random user, that is not the current user and is not already followed
             $userToFollow = User::orderByRaw("random()")->whereNotIn('users.id', $followed)->first();
             $user->following()->attach($userToFollow);
             $followed[] = $userToFollow->id;
         }
     });
 }
 public function update($userId, $roundId, UpdateRoundRequest $request)
 {
     $teeSet = TeeSet::where('course_id', '=', $request->course)->where('tee_type_id', '=', $request->teeType)->first();
     $date = Carbon::createFromDate($request->year, $request->month, $request->day);
     $round = $this->getRound($roundId, $userId);
     $round->date = $date->toDateString();
     $round->tee_set_id = $teeSet->id;
     $scores = $round->scores;
     for ($i = 1; $i <= 18; $i++) {
         $score = $scores[$i - 1];
         $score->strokes = $request->scores[$i];
         $score->putts = $request->putts[$i];
         $score->gir = null;
         $score->fairway = null;
         $hole = Hole::where('course_id', '=', $request->course)->where('number', '=', $i)->first();
         $score->hole_id = $hole->id;
         $score->save();
     }
     $round->save();
     return $round;
 }
 public function getTeeSet($courseId, $teeTypeId)
 {
     return TeeSet::where('course_id', '=', $courseId)->where('tee_type_id', '=', $teeTypeId)->first();
 }