【问题标题】:When I submit a form on Laravel I get error message: MethodNotAllowedHttpException in RouteCollection.php line 219?当我在 Laravel 上提交表单时,我收到错误消息:RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException?
【发布时间】:2016-08-25 15:29:38
【问题描述】:

当我提交表单时,我收到以下错误:“RouteCollection.php 第 219 行中的 MethodNotAllowedHttpException”。

我该如何解决这个问题?我查看了各种论坛和其他帖子,其中已经回答了这个问题,但没有一个对我有用。

我的路由文件如下:

<?php
/*
This is the file in which the rules for how users will use the application are kept
*/

Route::get('/',function() {
    return view('welcome');
});


Route::auth();

Route::get('/home', 'HomeController@index');

Route::resource('/questionnaires', 'QuestionnairesController');

Route::resource('/questions', 'QuestionsController');

Route::resource('/answers', 'AnswersController');

\

我的 create.blade.php 文件和表单如下所示:

@extends('layouts.master')

@section('title', 'Create Questionnaire | SurveySays!')

@section('content')
    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
        <h1>Create Questionnaire</h1>
        <h3>Create your questionnaire using the form below. Give it a title, a small description and write your ethical considerations:</h3>

        @if($errors->any())
            <div class="alert alert-danger">
                @foreach($errors->all() as $error)
                    <p>{{ $error }}</p>
                @endforeach
            </div>
        @endif

        {!! Form::open(array('url' => '/questionnaires/create')) !!}

        <div class="container-fluid">
            <div class="form-group">
                {!! Form::label('title', 'Title:') !!}
                {!! Form::text('title',null,['id' => 'title','class' => 'form-control']) !!}
            </div>
            <div class="form-group">
                {!! Form::label('description', 'Description:') !!}
                {!! Form::textarea('description',null,['class' => 'form-control']) !!}
            </div>
            <div class="form-group">
                {!! Form::label('ethics', 'Ethical considerations:') !!}
                {!! Form::textarea('ethics',null,['class' => 'form-control']) !!}
            </div>
            <div class="form-group">
                {!! Form::submit('Create', array('class' => 'btn btn-success form-control')) !!}
            </div>
        </div>
    </div>

    {!! Form::close() !!}
@endsection

谢谢:)

【问题讨论】:

    标签: forms laravel


    【解决方案1】:

    您应该改用store 方法。另外,使用路由,而不是 URL(你不想硬编码):

    {!! Form::open(array('route' => 'questionnaires.store') !!}
    

    create 方法用于向用户显示表单。而store 用于验证用户输入数据并将其持久化到数据库中。

    更多关于RESTful resource controllers here

    【讨论】:

    • 我改成这个并再次提交表单,我现在收到这个错误 SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into questionnairestitledescriptionethicsupdated_atcreated_at)值(标题、描述、道德、2016-04-30 14:33:59、2016-04-30 14:33:59 ))
    • 即使我提交了表单,我的数据库表也是空的
    • 表是空的,因为你得到了我猜的错误。您收到错误消息,因为您创建了没有时间戳的迁移。现在,您应该重新创建此表,或者如果您不需要时间戳,请将其添加到您的模型中:public $timestamps = false; - laravel.com/docs/5.2/eloquent#eloquent-model-conventions
    • 我的迁移中有时间戳吗? $table->timestamp('start_date'); $table->timestamp('end_date');
    • 看看你上面的错误信息。 Laravel 找不到 updated_at。您应该将$table-&gt;timestamps(); 添加到迁移中,将其回滚并再次迁移。 laravel.com/docs/master/migrations#migration-structure - 或者在你的模型中使用 public $timestamps = false;
    【解决方案2】:

    因为create 只允许 GET 请求显示表单。

    https://laravel.com/docs/5.2/controllers#restful-resource-controllers

    'method' =&gt; 'post' 添加到您的Form::open() 数组并将其指向/questionnaires

    【讨论】:

    • 我原来的答案不正确。您必须通过一种特定的方式与魔法资源控制器进行交互,包括您如何提交信息。仔细阅读文档;您还必须将操作指定为'create'
    • 真的很抱歉编辑,点击了错误的答案。回滚。
    • 没有戏剧性,我认为你的解决方案更简洁。
    【解决方案3】:

    我看到您没有设置任何 POST 路由,这意味着您无法向您的 Laravel 站点发送 HTTP POST 请求。

    如果你加上这个就可以了。

    Route::post('/questionnaires/create', 'QuestionnairesController@create');
    

    这将使用函数create() 设置到QuestionnairesController 的路由(HTTP POST 请求)。

    希望这行得通!

    【讨论】:

    • 感谢这工作!但是,现在我已经提交了表单,我刚刚返回到 create.blade
    • 可以在create函数上设置一个return,代码为:return redirect(url('/questionnaires'));如果答案对您有用,您可以将其标记为“有效”吗?
    • create 使用 GET 方法。 store 使用 POST。 OP 正在使用资源控制器,所以他不应该手动添加 POST/create 方法。 laravel.com/docs/5.1/controllers#restful-resource-controllers
    • 那我应该用什么?因为 POST 有效(例如,当我单击提交时,我没有再次收到错误消息)。但是,我看不到我在 /questionnaires 页面上创建的问卷
    • 你可以使用 Jaw.sh,这是一个标准的 Laravel 函数。
    猜你喜欢
    • 2016-06-28
    • 1970-01-01
    • 2016-08-30
    • 2016-06-15
    • 2016-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    相关资源
    最近更新 更多