在RouteCollection.php第219行中获取MethodNotAllowedHttpException:在laravel上

时间:2016-01-28 13:35:28

标签: php laravel laravel-5 laravel-5.1 laravel-routing

我得到上面的错误。我尝试在其他论坛上阅读同样的问题,但没有运气。我的创建,存储和编辑工作正常。但是在更新我的表单时我得到上面的错误。有人可以帮我这个。感谢

{!! Form::model($enrollment['method'=>'POST','route'=>['/enrollment',$enrollment->id],'class'=>'form-horizontal']) !!}

                <div class="form-group">
                    <label for="subject_code" class="col-md-3 control-label">Subject Code</label>
                    <div class="col-md-8">
                        <select class="form-control" name="_method" value="PUT" id="subject_code">
                            <option value="{{ $enrollment->subject_code }}">{{ $enrollment->subject_code }}</option>
                            @foreach($subjects as $subject)
                                <option value="{{ $subject->subject_code }}">{{ $subject->subject_code}}</option>
                            @endforeach 
                        </select>
                    </div>

                </div>

                <div class="form-group">
                    <label for="subject_description" class="col-md-3 control-label">Subject description</label>
                    <div class="col-md-8">
                        <select class="form-control" name="subject_description" id="subject_description">
                            <option value="{{ $enrollment->subject_description }}">{{ $enrollment->subject_description }}</option>
                            @foreach($subjects as $subject)
                                <option value="{{ $subject->subject_description }}">{{ $subject->subject_description}}</option>
                            @endforeach 
                        </select>
                    </div>

                </div>


                <div class="form-group">
                    <label for="subject_code" class="col-md-3 control-label">Subject Code</label>
                    <div class="col-md-8">
                        <select class="form-control" name="subject_code" id="subject_code">
                            <option value="{{ $enrollment->section }}">{{ $enrollment->section}}</option>
                            @foreach($sections as $section)
                                <option value="{{ $section }}">{{ $section }}</option>
                            @endforeach
                        </select>
                    </div>

                </div>


                <div class="form-group">
                    <label for="subject_code" class="col-md-3 control-label">Subject Code</label>
                    <div class="col-md-8">
                        <select class="form-control" name="subject_code" id="subject_code">
                            <option value="{{ $enrollment->schedule }}">{{ $enrollment->schedule }}</option>
                            @foreach($subjects as $subject)
                                <option value="{{ $subject->schedule }}">{{ $subject->schedule}}</option>
                            @endforeach 
                        </select>
                    </div>

                </div>  

                <div class="form-group">
                    <label for="subject_code" class="col-md-3 control-label">Subject Code</label>
                    <div class="col-md-8">
                        <select class="form-control" name="subject_code" id="subject_code">
                            <option value="{{ $enrollment->no_of_units }}">{{ $enrollment->no_of_units }}</option>
                            @foreach($subjects as $subject)
                                <option value="{{ $subject->no_of_units }}">{{ $subject->no_of_units}}</option>
                            @endforeach 
                        </select>
                    </div>

                </div>
                <div class="form-group">
                    <div class="col-md-7 col-md-offset-3">

                        <button type="submit" class="btn btn-success">
                            <i class="fa fa-save"></i>
                            &nbsp;Save Changes
                        </button>

                        <button type="submit" class="btn btn-danger">
                            <i class="fa fa-times-circle"></i>
                            &nbsp;Delete
                        </button>
                    </div>
                </div>                                                                              
{!! Form::close() !!}

这是我的EnrollmentController:

    public function update(EnrollmentRequest $request, $id)
{
    $enrollment = Enrollment::findOrFail($id);
    $enrollment->update($request->all());
    return redirect('/enrollment');
}

routes.php文件

// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');

// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');

Route::resource('enrollment','EnrollmentController');

1 个答案:

答案 0 :(得分:1)

我认为问题在于您的HTML中存在错误。在你的第一个&#34; subject_code&#34;输入,您有以下HTML:

<select class="form-control" name="_method" value="PUT" id="subject_code">

您不小心将此输入命名为_method,这是Laravel方法欺骗的保留输入名称。即使您已将值分配为&#34; PUT&#34;,这也不是选择工作的方式,并且该值最终将成为其他内容。这样可以防止Laravel方法欺骗工作正常。由于欺骗方法不起作用,您将向enrollment/{id}发送POST请求,并且该路由不允许POST请求。

您需要更正此输入,以便它未被命名为&#34; _method&#34;:

<select class="form-control" name="subject_code" id="subject_code">

此外,您需要修复Form::model()声明。它需要采用两个参数,第一个是模型,第二个是属性数组。在您的属性数组中,您需要&#34;方法&#34;成为&#34; PUT&#34; (这样表单生成器会自动创建隐藏的&#34; _method&#34;输入),你需要&#34; route&#34;中的第一个元素。 array是路由的名称,而不是url。更新如下:

{!! Form::model($enrollment, ['method' => 'PUT', 'route' => ['enrollment.update', $enrollment->id], 'class' => 'form-horizontal']) !!}