public function getCompleted(Request $request) { if ($request->has('user_id')) { // get battles by user $user_id = $request->input('user_id'); $battles = User::find($user_id)->battles()->completed(); } else { // get all battles $battles = Battle::completed(); } $data = $this->createBattleOverview($battles->get()); $amount = $request->input('amount', 15); $page = $request->input('page', 1); // slice requested data set for paginator $slicedData = array_slice($data, $amount * ($page - 1), $amount); return new LengthAwarePaginator($slicedData, count($data), $amount, $page); }
/** * Test for scopeOpenVoting and scopeCompleted */ public function testScopesOpenCompleted() { // create rappers $user1 = factory(App\Models\User::class)->create(); $user2 = factory(App\Models\User::class)->create(); $votingperiod = config('rap-battle.votingperiod', 24); // battles older than this date are closed: $timeoldest = new Carbon(); $timeoldest->subHours($votingperiod + 1); // create two battles $battle1 = new Battle(); $battle1->rapper1_id = $user1->id; $battle1->rapper2_id = $user2->id; $battle1->save(); $battle2 = new Battle(); $battle2->rapper1_id = $user1->id; $battle2->rapper2_id = $user2->id; $battle2->created_at = $timeoldest->toDateTimeString(); $battle2->save(); // test scopeOpenVoting // get battles from database $openBattles = Battle::openVoting()->get()->values()->keyBy('id'); $this->assertTrue($openBattles->has($battle1->id)); $this->assertFalse($openBattles->has($battle2->id)); // test scopeCompleted // get battles from database $completedBattles = Battle::completed()->get()->values()->keyBy('id'); $this->assertFalse($completedBattles->has($battle1->id)); $this->assertTrue($completedBattles->has($battle2->id)); }