suhas gawde suhas gawde - 23 days ago 10
PHP Question

MethodNotAllowedHttpException while submitting form to process using laravel?

I am new to laravel and trying to create a simple application that is blog, i wanted to store a post details into database. i have setup my database and tables also using laravel commands.
i have created a following form in order to store the data into database :

@extends('main')

@section('title','| Create Post')
@endsection

@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Create New Post</h1>
<hr/>
<form action="store" method="POST">
<div class="form-group">
<div class="form-group">
<label name="title">Title:</label>
<input id="title" name="title" class="form-control">
</div>
<div class="form-group">
<label name="body">Post Body:</label>
<textarea id="body" name="body" class="form-control"></textarea>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="POST">
<input type="submit" class="btn btn-success btn-lg btn-block" value="Save Post">
</div>
</form>
</div>
</div>
@endsection


i have create a route rule for this form submit request as follows :

Route::resource('posts','PostController');


where PostController is controller where all required resource method is implemented for processing and saving data as

<?php namespace App\Http\Controllers;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Post;
use Illuminate\Http\Request;

class PostController extends Controller {

public function index()
{
//
}

public function create()
{
return view('posts.create');
}

public function store(Request $request)
{
//validate the data
$this->validate($request,array(
'title' => 'required|max:255',
'body' => 'required'
));

//store in the database
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();

//redirect to page
return redirect()->route('posts.show',$post->id);
}

public function show($id)
{
//
}

public function edit($id)
{
//
}

public function update($id)
{
//
}

public function destroy($id)
{
//
}

}


i have also list down my route using command prompt which gives me following output :

App\Http\Controllers\PagesController@getIndex | |
| | GET|HEAD | posts | posts.index | App\Http\Controllers\PostController@index | |
| | GET|HEAD | posts/create | posts.create | App\Http\Controllers\PostController@create | |
| | POST | posts | posts.store | App\Http\Controllers\PostController@store | |
| | GET|HEAD | posts/{posts} | posts.show | App\Http\Controllers\PostController@show | |
| | GET|HEAD | posts/{posts}/edit | posts.edit | App\Http\Controllers\PostController@edit | |
| | PUT | posts/{posts} | posts.update | App\Http\Controllers\PostController@update | |
| | PATCH | posts/{posts} | | App\Http\Controllers\PostController@update | |
| | DELETE | posts/{posts} | posts.destroy | App\Http\Controllers\PostController@destroy

i am getting MethodNotAllowedHttpException after submitting the form for saving the details.

Answer

Try this:

@section('content')
<div class="row">
    <div class="col-md-8 col-md-offset-2">
        <h1>Create New Post</h1>
        <hr/>
        <form action="{{ route('posts.store') }}" method="POST">
            <div class="form-group">
                <div class="form-group">
                    <label name="title">Title:</label>
                    <input id="title" name="title" class="form-control">
                </div>
                <div class="form-group">
                    <label name="body">Post Body:</label>
                    <textarea id="body" name="body" class="form-control"></textarea>
                </div>
                <input type="hidden" name="_token" value="{{ csrf_token() }}">
                <input type="hidden" name="_method" value="POST">
                <input type="submit" class="btn btn-success btn-lg btn-block" value="Save Post">
            </div>
        </form>
    </div>
</div>
@endsection

Docs