简体   繁体   English

在Laravel中在RouteCollection.php第219行中获取MethodNotAllowedHttpException

[英]Getting MethodNotAllowedHttpException in RouteCollection.php line 219: on laravel

Im getting the errors above. 我得到上面的错误。 I tried to read on other forums with the same problem but with no luck. 我试图在其他论坛上阅读同样的问题,但没有运气。 My create, store and edit are working. 我的创建,存储和编辑正在运行。 However when updating my form im getting the error above. 但是,当更新我的表单时,我得到了上面的错误。 Can someone help me on this. 有人可以帮我吗 Thanks 谢谢

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

Here's my EnrollmentController: 这是我的EnrollmentController:

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

routes.php 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');

I think the issue is that you have a mistake in your HTML. 我认为问题在于您的HTML错误。 In your very first "subject_code" input, you have the following HTML: 在您的第一个“ subject_code”输入中,您具有以下HTML:

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

You've accidentally named this input as _method , which is the reserved input name for Laravel method spoofing. 您无意中将此输入命名为_method ,这是Laravel方法欺骗的保留输入名称。 Even though you've assigned the value as "PUT", this is not how selects work, and the value will end up being something else. 即使您已将值分配为“ PUT”,但这也不是select的工作方式,该值最终将变为其他内容。 This is preventing the Laravel method spoofing from working correctly. 这会阻止Laravel方法欺骗正常工作。 Since the method spoofing is not working, you're sending a POST request to enrollment/{id} , and that route does not allow POST requests. 由于方法欺骗无效,因此您正在向POST发出一个POST请求到enrollment/{id} ,并且该路由不允许POST请求。

You need to correct this input so that it is not named "_method": 您需要更正此输入,以使其不命名为“ _method”:

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

Additionally, you need to fix your Form::model() statement. 此外,您需要修复Form::model()语句。 It needs to take two parameters, where the first is the model, and the second is an array of attributes. 它需要两个参数,第一个是模型,第二个是属性数组。 In your array of attributes, you need the "method" to be "PUT" (so that the form builder will automatically create the hidden "_method" input), and you need the first element in the "route" array to be the name of the route, not the url. 在属性数组中,您需要将“方法”设置为“ PUT”(以便表单构建器将自动创建隐藏的“ _method”输入),并且需要将“ route”数组中的第一个元素作为名称路线,而不是网址。 Updates shown below: 更新如下所示:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RouteCollection.php 第 219 行中的 Laravel 5 MethodNotAllowedHttpException - Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 219 Laravel 5:RouteCollection.php第219行中的MethodNotAllowedHttpException - Laravel 5: MethodNotAllowedHttpException in RouteCollection.php line 219 Laravel 5在RouteCollection.php第219行中的MethodNotAllowedHttpException: - Laravel 5 MethodNotAllowedHttpException in RouteCollection.php line 219: Laravel API 中 RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException - MethodNotAllowedHttpException in RouteCollection.php line 219 in laravel API's Laravel 5.2:RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException - Laravel 5.2 : MethodNotAllowedHttpException in RouteCollection.php line 219 Laravel 错误:RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException - Laravel error: MethodNotAllowedHttpException in RouteCollection.php line 219 在RouteCollection.php第219行中的Laravel 5.2 MethodNotAllowedHttpException - Laravel 5.2 MethodNotAllowedHttpException in RouteCollection.php line 219 RouteCollection.php 219行中的MethodNotAllowedHttpException错误laravel: - MethodNotAllowedHttpException in RouteCollection.php line 219 error laravel: Laravel 5.1.26:RouteCollection.php第219行中的MethodNotAllowedHttpException - Laravel 5.1.26 : MethodNotAllowedHttpException in RouteCollection.php line 219 RouteCollection.php第219行中的MethodNotAllowedHttpException - MethodNotAllowedHttpException in RouteCollection.php line 219
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM