sgt_disco
sgt_disco

Reputation: 173

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}', 'OrderController@edit')->name('orders.edit');
Route::patch('orders/update', 'OrderController@update')->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' => ['OrderController@update', $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?

Upvotes: 1

Views: 836

Answers (1)

rpm192
rpm192

Reputation: 2454

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

{!! Form::open(['action' => ['OrderController@update', $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', 'OrderController@update')->name('orders.update');

// to
Route::post('orders/update/{order}', 'OrderController@update')->name('orders.update');

Upvotes: 3

Related Questions