更新数据Laravel时RouteCollection.php第251行中的MethodNotAllowedHttpException

时间:2018-08-05 08:05:32

标签: php laravel laravel-5 laravel-routing

此问题将要编辑我表中的数据。视图或表单已完美显示。但是当我编辑我的数据并按更新按钮时,它显示MethodNotAllowedHttpException错误。我的方法有误吗?

这是视图

<form class="form-horizontal form-label-left" method="post" action="{{action('AdminController@updateruang', $id_ruang)}}">
    {{csrf_field()}}
    <div class="item form-group">
        <input name="_method" type="hidden" value="PATCH">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">ID Ruangan <span class="required">*</span></label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="title" name="title" value="{{$ruang->id_ruang}}">
        </div>
    </div>
    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">Nama Ruangan <span class="required">*</span></label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="title" name="title" value="{{$ruang->nm_ruang}}">
        </div>
    </div>
    <div class="item form-group">
        <label class="control-label col-md-3 col-sm-3 col-xs-12">Keterangan <span class="required">*</span></label>
        <div class="col-md-6 col-sm-6 col-xs-12">
            <input type="text" class="form-control form-control-lg" id="lgFormGroupInput" placeholder="title" name="title" value="{{$ruang->keterangan}}">
        </div>
    </div>
    <div class="form-group row">
        <div class="col-md-2"></div>
            <button type="submit" class="btn btn-primary">Update</button>
        </div>
  </form>

这是AdminController

public function updateruang(Request $request, $id_ruang)
{
    $ruang = Ruang::find($id_ruang);
    $ruang->nm_ruang = $request->get('nm_ruang');
    $ruang->keterangan = $request->get('keterangan');
    $ruang->save();

    return redirect('/admin/dataruang')->with('success', 'New data has been updated!!');
}

这是路线

Route::post('/editdataruang{id_ruang}','AdminController@updateruang', function(){
    $users[] = Auth::user();
    $users[] = Auth::guard()->user();
    $users[] = Auth::guard('admin')->user();

})->name('editdataruang');

1 个答案:

答案 0 :(得分:0)

问题是这样的-在您的代码形式中,您拥有:

<input name="_method" type="hidden" value="PATCH">

但您的路线定义为

Route::post('/editdataruang{id_ruang}','AdminController@updateruang', function(){

因此,请删除行:

<input name="_method" type="hidden" value="PATCH">

来自表单或更改:

Route::post('/editdataruang{id_ruang}','AdminController@updateruang', function(){

进入

Route::patch('/editdataruang{id_ruang}','AdminController@updateruang', function(){

假设您要进行更新,则应该将路由从post更改为patch,而不是从表单中删除行。