public function update(TournamentTeamRequest $request, TournamentTeam $tournament_teams) { foreach ($request->all()['data']['attributes'] as $key => $value) { if (isset($tournament_teams->{$key})) { $tournament_teams->{$key} = $value; } } // fetch tournament $tournament = Tournament::findOrFail($request->all()['data']['relationships']['tournament']['data']['id']); $tournament_teams->tournament_id = $tournament->id; $tournament_teams->save(); $resource = new Item($tournament_teams, new TournamentTeamTransformer(), 'tournament-teams'); return $this->fractal()->createData($resource)->toJson(); }
public function testPatchTournamentTeams() { // create user info and convert it to json $location = factory(Location::class)->create(); $tournament = factory(Tournament::class)->create(['location_id' => $location->id]); $tournamentTeamObject = factory(TournamentTeam::class)->create(['tournament_id' => $tournament->id]); $tournamentTeamObject->name = 'My Awesome Team'; $tournamentTeamData = json_encode(['data' => ['type' => 'tournament-teams', 'attributes' => $tournamentTeamObject->toArray(), 'relationships' => ['tournament' => ['data' => ['type' => 'tournaments', 'id' => $tournament->id]]]]]); $response = $this->callPatch(self::API_URL . $tournamentTeamObject->id, $tournamentTeamData); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); $this->assertNotNull($tournamentTeam = json_decode($response->getContent())->data); $this->assertNotEmpty($tournamentTeam->id); // test to make sure the user was created $updatedTournamentTeam = TournamentTeam::findOrFail($tournamentTeam->id); $this->assertEquals('My Awesome Team', $updatedTournamentTeam->name); $this->assertEquals($updatedTournamentTeam->tournament->toArray(), $tournament->toArray()); }