Ad

Laravel - Update Always Throws "MethodNotAllowedHttpException No Message"

- 1 answer

From my research, this error is related to a routing error. As this error is thrown regardless of the code I utilize in my update function, that makes sense.

However I believe all my routes/forms are proper:

web.php

Route::get('orders/edit/{order}', '[email protected]')->name('orders.edit');
Route::patch('orders/update', '[email protected]')->name('orders.update');

OrderController.php

public function edit(Order $order)
{
    $user = Auth::user();
    $meals = DB::table('products')
              ->leftJoin('order_product', function ($join) use ($order) {
                  $join->on('products.id', '=', 'order_product.product_id')
                       ->where('order_product.order_id', '=', $order->id);
              })
              ->select('products.*', 'order_product.qty')
              ->get();

    return view('orders.edit', compact('order', 'products', 'meals'));
}

public function update(Request $request,Order $order)
{
    //doesnt seem to matter what is in here but this is my return
    return redirect()->route('orders.checkout', $order->id);
}

edit.blade.php

{!! Form::open(['route' => 'orders.update', 'method' => 'patch']) !!}
    {{ Form::text('coupon', $order->coupon) }} // sample of an input I am using
    {{ Form::submit('Place Order') }}
{!! Form::close() !!}

UPDATE

per the suggestion of @rpm192, I attempted:

{!! Form::open(['action' => ['orders.update', $order->id], 'method' => 'patch']) !!}

but then when loading the edit.blade.php, it throws me...

Action App\Http\Controllers\orders.update not defined. (View: /Users/now/Public/keto/resources/views/orders/edit.blade.php)

so I then tried...

{!! Form::open(['action' => ['[email protected]', $order->id], 'method' => 'patch']) !!}

but that throws me...

Too few arguments to function App\Http\Controllers\OrderController::update(), 1 passed and exactly 2 expected

assuming this is the correct method as I am now getting closer to the solution, what additional argument is it looking for?

Ad

Answer

Your controller requires both the request and the ID of the order (so it knows which one to update).

{!! Form::open(['action' => ['[email protected]', $order->id], 'method' => 'post']) !!} // modified this
    {{ Form::text('coupon', $order->coupon) }}

    {{ Form::hidden('_method', 'PUT') }} // added this

    {{ Form::submit('Place Order') }}
{!! Form::close() !!}

If that doesn't work, try again by modifying your route:

// from
Route::patch('orders/update', '[email protected]')->name('orders.update');

// to
Route::post('orders/update/{order}', '[email protected]')->name('orders.update');
Ad
source: stackoverflow.com
Ad