ramonph's avatar

The PATCH method is not supported for route jobs/105. Supported methods: GET, HEAD.

hi,

I hope somebody can help me out with this. I have been trying and trying but still could figure it out.

Excerpts from my calling form : form method="POST" action="/jobs/{{ $job->id }}" @csrf @method('patch') ...

button type="submit" class="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg- indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" Update button

excerpts from my web.php

Route::patch('/jobs/{$id}',function($id) { // update the record // steps //validate //authorize //update and persist // redirect

request()->validate([
    'title' =>['required','min:3'], 
    'salary'=>['required']
]);
$job = Job::findOrFail($id);

$job->update ([
    'title'=>request('title'),
    'salary'=>request('salary')
]);

return redirect('/jobs/'.$job->id);

});

My route:list GET|HEAD contact .................................................................................. GET|HEAD jobs ..................................................................................... POST jobs ..................................................................................... GET|HEAD jobs/create .............................................................................. PATCH jobs/{$id} ............................................................................... DELETE jobs/{$id} ............................................................................... GET|HEAD jobs/{id} ................................................................................ GET|HEAD jobs/{id}/edit ...........................................................................

So in a nutshell, i have included the blade directive to use patch method, i have included in my route the patch method and checked my route list and it is included as well.

My suspect is my web.php route statement but i cannot figure out what is wrong.

Hope someone can show me what i missed.

Best Regards, Mon

p.s I have been followng the 30 days to learn laravel by jefferey.

0 likes
5 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

The issue you're encountering is likely due to the incorrect use of curly braces in your route definition. In Laravel, when defining route parameters, you should use curly braces without the dollar sign. Here's how you can fix it:

Correct Route Definition

In your web.php, update the route definition to use the correct syntax for route parameters:

Route::patch('/jobs/{id}', function ($id) {
    // Validate the request
    request()->validate([
        'title' => ['required', 'min:3'],
        'salary' => ['required']
    ]);

    // Find the job by ID
    $job = Job::findOrFail($id);

    // Update the job with the new data
    $job->update([
        'title' => request('title'),
        'salary' => request('salary')
    ]);

    // Redirect to the updated job's page
    return redirect('/jobs/' . $job->id);
});

Additional Considerations

  1. Form Method: Ensure that your form in the Blade template is using the PATCH method. You can do this using the @method directive in a form that uses POST:

    <form action="/jobs/{{ $job->id }}" method="POST">
        @csrf
        @method('PATCH')
        <!-- form fields here -->
        <button type="submit">Update</button>
    </form>
    
  2. CSRF Token: Make sure you include the CSRF token in your form using @csrf.

  3. Route List: After making these changes, run php artisan route:list to ensure that the route is correctly registered.

By correcting the route parameter syntax and ensuring your form is set up correctly, the PATCH method should be supported for your route.

1 like
Snapey's avatar

Don't put $ in your route definitions.

eg , should be

Route::patch('/jobs/{id}',function($id)
AbdulrahmanAbdullahkh's avatar

HI I am having the same problem , and when am trying to solve it the same way lil lary said it gives me a 404 error , what do yall think , it been two full hours trying to solve this

Please or to participate in this conversation.