Laravel-更新总是抛出“ MethodNotAllowedHttpException No message”

时间:2018-12-13 18:44:29

标签: laravel laravel-5 laravel-routing

根据我的研究,此错误与路由错误有关。不管我在更新函数中使用什么代码,都会抛出此错误,这很有意义。

但是我认为我所有的路线/表格都是正确的:

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() !!}

更新

根据@ rpm192的建议,我尝试:

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

但是随后在加载edit.blade.php时,它会抛出我...

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

所以我然后尝试了...

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

但是那把我扔了...

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

假设这是正确的方法,因为我现在越来越接近解决方案,它还在寻找什么附加参数?

1 个答案:

答案 0 :(得分:3)

您的控制器同时要求请求和订单ID(因此知道要更新的订单)。

{!! 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() !!}

如果这不起作用,请通过修改路线重试:

// from
Route::patch('orders/update', 'OrderController@update')->name('orders.update');

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