【问题标题】:Getting MethodNotAllowedHttpException in RouteCollection.php line 219: on laravel在 RouteCollection.php 第 219 行获取 MethodNotAllowedHttpException:在 laravel 上
【发布时间】:2016-05-05 22:01:02
【问题描述】:

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

{!! 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');

【问题讨论】:

  • 这通常发生在你有相同的 get 和 post 方法路径时。
  • @MasterSith 那么解决方案是什么?
  • 我知道您正在使用资源,这很好,但要为新功能创建新路线。像 Rouse::POST('update', 'Controller@update');也使用大写后者
  • @MasterSith POST 无效请记住。它应该是小写字母。
  • 你能发布堆栈跟踪吗?

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


【解决方案1】:

我认为问题在于您的 HTML 有错误。在您的第一个“subject_code”输入中,您有以下 HTML:

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

您不小心将此输入命名为 _method,这是 Laravel 方法欺骗的保留输入名称。即使您已将值指定为“PUT”,这也不是选择的工作方式,并且该值最终会变成其他东西。这会阻止 Laravel 方法欺骗正常工作。由于方法欺骗不起作用,您正在向enrollment/{id} 发送 POST 请求,而该路由不允许 POST 请求。

您需要更正此输入,使其不被命名为“_method”:

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

此外,您需要修复您的Form::model() 语句。它需要带两个参数,第一个是模型,第二个是属性数组。在您的属性数组中,您需要“方法”为“PUT”(这样表单构建器将自动创建隐藏的“_method”输入),并且您需要“路线”数组中的第一个元素作为名称路线,而不是网址。更新如下:

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

【讨论】:

  • 哦,是的,我没有注意到这一点。谢谢你的回答。我想你忘记在 $enrollment 上加冒号了?
  • {!! Form::model($enrollment,['method'=>'PUT','route'=>['/enrollment',$enrollment->id],'class'=>'form-horizo​​ntal']) !! } shareeditflag 10 分钟前回答
  • {!! Form::model($enrollment,['method'=>'PUT','route'=>['/enrollment',$enrollment->id],'class'=>'form-horizo​​ntal']) !! } 不适合我我得到 Route [/enrollment] 未定义。
  • 得到它现在它正在工作谢谢你的努力和回答。
猜你喜欢
  • 2016-08-30
  • 2016-06-15
  • 2016-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多