Laravel - Update Always Throws "MethodNotAllowedHttpException No Message"
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?
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');
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms