0
votes

I have the following code in my controller:

public function update(Request $request, $id)
{
    $cook = cooks::findOrFail($id);

    if (!$cook) {
        return response()->json([
            'success' => false,
            'message' => 'Sorry, cook with id ' . $id . ' cannot be found',
        ], 400);
    }

    $updated = $cook->fill($request->all())
        ->save();

    if ($updated) {
        return response()->json([
            'success' => true,
        ]);
    } else {
        return response()->json([
            'success' => false,
            'message' => 'Sorry, cook could not be updated',
        ], 500);
    }
}

And when I use in my postman method PUT I received this message "The PUT method is not supported for this route. Supported methods: GET, HEAD, POST."

in my api.php I have the following line:

Route::resource('cook', 'cookList');

and here is the route that I use in postman:

`http://127.0.0.1:8000/api/cook`

with body id-1 and title-cook Can someone help me, please?

2
What url did you use to execute the PUT request? /api/cook/1? And you should fix the cooks::findOrFail to Cook::findOrFailMaartenDev
Please provide the code that you calling your route.AH.Pooladvand
sorry for that, I updated my articleMindru Ion

2 Answers

3
votes

You are missing a parameter in your route. As you are using http://127.0.0.1:8000/api/cook it's guessing you are trying to go to the index method or store method. So add a id parameter to your route and it should work with PUT method.

http://127.0.0.1:8000/api/cook/1

(Not tested, but should work)