public function update(TeamMemberRequest $request, TeamMember $team_members) { $data = $request->all(); foreach ($data['data']['attributes'] as $key => $value) { if (isset($team_members->{$key})) { $team_members->{$key} = $value; } } // fetch team $team = Team::findOrFail($data['data']['relationships']['team']['data']['id']); $team_members->team_id = $team->id; // fetch user $user = User::findOrFail($data['data']['relationships']['user']['data']['id']); $team_members->user_id = $user->id; $team_members->save(); $resource = new Item($team_members, new TeamMemberTransformer(), 'team-members'); return $this->fractal()->createData($resource)->toJson(); }
public function testPatchTeamMembers() { // create user info and convert it to json $team = factory(Team::class)->create(); $user = factory(User::class)->create(); $teamMemberObject = factory(TeamMember::class)->create(['team_id' => $team->id, 'user_id' => $user->id]); $teamMemberObject->position = 'czar'; $teamMemberData = json_encode(['data' => ['type' => 'team-members', 'attributes' => $teamMemberObject->toArray(), 'relationships' => ['team' => ['data' => ['type' => 'teams', 'id' => $team->id]], 'user' => ['data' => ['type' => 'users', 'id' => $user->id]]]]]); $response = $this->callPatch(self::API_URL . $teamMemberObject->id, $teamMemberData); $this->assertEquals(Response::HTTP_OK, $response->getStatusCode()); $this->assertNotNull($teamMember = json_decode($response->getContent())->data); $this->assertNotEmpty($teamMember->id); // test to make sure the user was created $updatedTeamMember = TeamMember::findOrFail($teamMember->id); $this->assertEquals('czar', $updatedTeamMember->position); $this->assertEquals($updatedTeamMember->team->toArray(), $team->toArray()); }