public function update(TournamentMemberRequest $request, TournamentMember $tournament_members) { foreach ($request->all()['data']['attributes'] as $key => $value) { if (isset($tournament_members->{$key})) { $tournament_members->{$key} = $value; } } // fetch tournament $tournament = Tournament::findOrFail($request->all()['data']['relationships']['tournament']['data']['id']); $tournament_members->tournament_id = $tournament->id; $tournament_members->save(); $resource = new Item($tournament_members, new TournamentMemberTransformer(), 'tournament-members'); return $this->fractal()->createData($resource)->toJson(); }
public function testPatchTournamentMembers() { // create user info and convert it to json $location = factory(Location::class)->create(); $tournament = factory(Tournament::class)->create(['location_id' => $location->id]); $user = factory(User::class)->create(); $tournamentMemberObject = factory(TournamentMember::class)->create(['tournament_id' => $tournament->id, 'user_id' => $user->id]); $tournamentMemberObject->position = 'custom name'; $tournamentMemberData = json_encode(['data' => ['type' => 'tournament-teams', 'attributes' => $tournamentMemberObject->toArray(), 'relationships' => ['tournament' => ['data' => ['type' => 'tournaments', 'id' => $tournament->id]]]]]); $response = $this->callPatch(self::API_URL . $tournamentMemberObject->id, $tournamentMemberData); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); $this->assertNotNull($tournamentMember = json_decode($response->getContent())->data); $this->assertNotEmpty($tournamentMember->id); // test to make sure the user was created $updatedTournamentTeam = TournamentMember::findOrFail($tournamentMember->id); $this->assertEquals('custom name', $updatedTournamentTeam->position); $this->assertEquals($updatedTournamentTeam->tournament->toArray(), $tournament->toArray()); }