skip to Main Content

I have a form when I update user data. When I press button update show me error The PUT method is not supported for route customers/5/edit. Supported methods: GET, HEAD.. I check my code but I can’t find error.

route

Route::post('store', 'AppHttpControllersCustomerController@store')->name('customers.store');

Route::get('/{customer}/edit', 'AppHttpControllersCustomerController@edit')->name('customers.edit');

Route::put('/{customer}', 'AppHttpControllersCustomerController@update')->name('customers.update');

controller

public function edit(Customer $customer)
{
    return view('edit', compact('customer'));
}

public function update(Customer $request, $id)
{
    $customers = Customer::find($id);
    $customers->first_name = $request->input('first_name');
    $customers->last_name = $request->input('last_name');      
    $customers->update();
    return redirect('/');
}

view

<form action="{{ route('customers.store') }}" method="post">
        <div class="form-outline mb-4">
            <input type="text" id="form4Example1" class="form-control" value="{{ $customer->first_name }}"/>
            <label class="form-label" for="form4Example1">First name</label>
        </div>

        <div class="form-outline mb-4">
            <input type="text" id="form4Example1" class="form-control" value="{{ $customer->last_name }}"/>
            <label class="form-label" for="form4Example1">Last name</label>
        </div>

    </form>

2

Answers


  1. Because your put end-point is called customers.update not customers.store.

    Your form action should be:

    action="{{ route('customers.update', [ 'id' => $customer->id ]) }}"
    

    Also take a look at Method Spoofing.

    On a side note, you would be better off using Resource Controllers to better enforce the CRUD standard.
    You can use Partials to trim down the full CRUD spec of a resource.

    Login or Signup to reply.
  2. Route method:

       Route::put('customers/update/{id}', 'AppHttpControllersCustomerController@update')->name('customers.update');
    

    Blade Logic

     <form action="{{ route('customers.update',[$customer->id]) }}" method="post">
            @method('PUT')
            @csrf
            <div class="form-outline mb-4">
                <input type="text" id="form4Example1" class="form-control" value="{{ $customer->first_name }}"/>
                <label class="form-label" for="form4Example1">First name</label>
            </div>
    
            <div class="form-outline mb-4">
                <input type="text" id="form4Example1" class="form-control" value="{{ $customer->last_name }}"/>
                <label class="form-label" for="form4Example1">Last name</label>
            </div>
    
        </form>
    

    Controller Logic:

    public function edit(Customer $customer)
    {
        return view('edit', compact('customer'));
    }
    
    public function update(Customer $request, $id)
    {
        $customers = Customer::find($id);
        $customers->first_name = $request->input('first_name');
        $customers->last_name = $request->input('last_name');      
        $customers->update();
        return redirect('/');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search