0
votes

I have a problem when I click edit button in the edit.blade.php. In update function the PUT method is not supported for this route. Supported methods: POST.

web.php

<?php

Route::get('/', function () {
    return view('welcome');
});
Route::get('phones','PhonesController@index');
Route::post('store','PhonesController@store');
Route::delete('destroy/{id}','PhonesController@destroy');
Route::put('edit/{id}', 'PhonesController@edit');
Route::post('update/{id}','PhonesController@update');

View edit.blade.php

<div class="container">
    <div class="col-sm-offset-2 col-sm-8">
        <div class="panel panel-default">
            <div class="panel-heading">
                Edit {{$phone->name}}
            </div>

            <div class="panel-body">
                <!-- Display Validation Errors -->
                <!-- New Task Form -->
            <form action="{{url('update/'.$phone->id)}}" method="POST" class="form-horizontal">
                        @csrf     


                <div class="form-group">
                    <div class="col-sm-offset-3 col-sm-6">    
                        <button type="submit" class="btn btn-primary">
@method('put')
                            <i class="fa fa-edit"></i> Edit
                        </button>
                    </div>
                </div>
                </form>
            </div>
        </div> 

and Controller

public function edit($id)
{
    $phone= Phone::find($id);

    return view('edit',compact('phone'));
}

public function update(Request $request,$id)
{
    $this->validate($request, [
        'name' => 'required|max:10',
        'model'=>'required'
    ]);

    $phone=new Phone();
    $phone->name=$request->input('name');
    $phone->model=$request->input('model');
    $phone->save();

    return back();
}

Just this is a problem I tried another ways but still getting same problem thank you.

1
You don't have <input name="_method" type="hidden" value="PUT"> in your form anywhere, do you?aynber
what edit button? there is nothing here that is sending a PUT request or a POST request with a spoofed _method as PUT, there is nothing here that can send a post request at alllagbox
i have but i clip it because too muck codeMohammed Wadee
edit your question with the information, dont add the code to the commentslagbox
you defined your 'update' route as POST, but you are sending a PUT request, as simple as that :-)lagbox

1 Answers

0
votes

Your edit route shouldn't be PUT, it should be GET. You should then adjust your update route to be PUT if you want to submit it that way:

Route::get('edit/{id}', 'PhonesController@edit');
Route::put('update/{id}','PhonesController@update');

Then what ever is sending you to the edit page can just be a regular hyper link (GET) which will allow you to redirect to it as well.