Laravel 5.1使用资源控制器的存储方法上的MethodNotAllowedHttpException [英] Laravel 5.1 MethodNotAllowedHttpException on store method using Resource Controller

查看:60
本文介绍了Laravel 5.1使用资源控制器的存储方法上的MethodNotAllowedHttpException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用资源控制器将记录添加到数据库,但是,出现MethodNotAllowedHttpException错误.我已经遇到过类似的问题,例如 this

I am trying to add a record to a database utilizing a resource controller, however, am getting MethodNotAllowedHttpException error. I have gone through several similar questions like this or that ,however, none seem to answer me. This is my code:

Routes.php

Routes.php

Route::resource('animals', 'AnimalsCtrl');

我的模型的一部分.

protected $table='animals';
protected $primaryKey='name';

protected $fillable = [
    'name', 'type'
];

控制器中的存储方法.

   public function store(Request $request)
    {
        $creature = $request->all();
        Animal::create($creature);

    }

这是表格.

        <form method="post">
            <div class="small-6 small-centered large-4 large-centered columns">
                {!! csrf_field() !!}
                <table>
                    <tr>
                        <th scope="row">Name</th>
                        <td>
                            <input type="text" name="name" maxlength="50" required>
                        </td>
                    </tr>
                    <tr>
                        <th scope="row">Type</th>
                        <td>
                            <input type="text" name="type" maxlength="20" required>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <button type="submit" class="button success">
                                <i class="fi-plus"></i>
                                Add Animal
                            </button>
                        </th>
                        <td>
                            <a href="{{url('/animals')}}" class="button alert">
                                <i class="fi-x-circle"></i>
                                &nbsp; Cancel
                            </a>
                        </td>
                    </tr>
                </table>
            </div>
        </form>

有人对我如何解决这个问题有任何建议吗?

Does anyone have any suggestion on how I can resolve this?

推荐答案

发布表单时,将表单发布到的URL是什么?网址应该在action中.例如如下

When you are posting the form, what is the URL that you are posting the form to ? The url should be in the action . For example as follows

<form action="/animals" method="post">
</form> 

这篇关于Laravel 5.1使用资源控制器的存储方法上的MethodNotAllowedHttpException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆