Ad

Laravel - Passing Parameter For Patch Request

- 1 answer

Hi i am working on a update method for updating a profile, right now i am updating the profile by passing the model as a parameter but i am wanting to pass in the id of a profile so the route is patch('/profiles/podcast/{id}'' i am quite new to laravel so im wondering how do i modify the controller and phpunit test to update this way when grabbing objects in laravel?

Update function in the controller:

 public function update(PodcastProfile $podcastProfile)
{
    $this->user = Auth::user();
    $this->podcastProfile = $podcastProfile;

    if (!$this->hasPodcastProfile()) {
        abort(400, "You don't have a podcast profile configured");
    }

    $this->validation();

    $this->transaction();

    return $this->podcastProfile->toJson();
}

This is the current route for the update method

Route::patch('/profiles/podcast/{podcastProfile}', '[email protected]');

This is the phpunit test case for the function

 /**
 * @test
 */
public function it_should_update_podcast_profile()
{
    $podcastDetails = $this->handlePostRequestToController();

    $this->json('patch', '/profiles/podcast/' . $podcastDetails['id'], $this->updateData)
        ->assertSuccessful();

    $this->checkPodcastProfile($this->updateData, $podcastDetails['id']);
    $this->checkGuestFormats($this->updateData['guest_format']);
    $this->checkAvailability($this->updateData['availability']);
    $this->checkEquipment($this->updateData['equipment']);
    $this->checkCategories($this->updateData['categories']);
    $this->checkLocation($this->updateData['country'], $this->updateData['city']);
}
Ad

Answer

Hej,

if I understand your question correctly you would just pass the id of that profile instead, just like you said:

Route::patch('/profiles/podcast/{podcastProfileId}','[email protected]');

and then fetch the profile by that given id.

so something like:

    $this->podcastProfile = App\PodcastProfile::find($podcastProfileId)

Also i feel like choosing the route-model-binding approach like @Remul described would be the better approach.

Ad
source: stackoverflow.com
Ad