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>
Cancel
</a>
</td>
</tr>
</table>
</div>
</form>
Does anyone have any suggestion on how I can resolve this?
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
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With