3

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

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

Part of my model.

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

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

The store method in the controller.

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

    }

This is the form.

        <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?

Community
  • 1
  • 1
Adwin
  • 195
  • 2
  • 6
  • 21

2 Answers2

2

I might be wrong but I think you are missing the action parameter in your form

Try this:

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

Instead of this

<form method="post">

As a tip I suggest you use the HTML Forms facade. Check this out: https://laracasts.com/series/laravel-5-fundamentals/episodes/10

Here's the documentation for Laravel 5.1 https://laravelcollective.com/docs/5.1/html

ccostel
  • 117
  • 1
  • 9
1

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> 
Gagan
  • 5,416
  • 13
  • 58
  • 86
  • If you think this answer has resolved your query , please mark this as an answer for people coming in next. - Thanks – Gagan Feb 08 '16 at 01:05