1
votes

Im having trouble posting my form to my database using laravel. When i click submit, it shows me the error MethodNotAllowedHttpException in RouteCollection.php line 218. My HTML code is shown below. I have defined the routes as shown below and I have also pasted my PostController which contains the store function.

<div class="blog-page blog-content-2">
        <div class="row">
            <div class="col-lg-9">
                <div class="blog-single-content bordered blog-container">
                    <div class="blog-comments">
                        <h3 class="sbold blog-comments-title">Leave A Comment</h3>
                        <form method="post" action="store">
                            <div class="form-group">
                                <input name="title" type="text" placeholder="Your Name" class="form-control c-square"> </div>                      
                            <div class="form-group">
                                <textarea name="body" rows="8" name="message" placeholder="Write comment here ..." class="form-control c-square"></textarea>
                            </div>
                            <div class="form-group">
                                <button type="submit" class="btn blue uppercase btn-md sbold btn-block">Submit</button>
                            </div>
                        </form>
                    </div>
                </div>      
            </div>
        </div>
    </div>

This is my route page

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

This is the PostController which contains the store function which is suppose to store the data into the database.

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

            //Store the data into the database
            $post = new Post;
            $post->title = $request->get('title');
            $post->body = $request->get('body');
            $post->save();

            //redirect to another page
            return redirect()->route('posts.show', $post->id);
        }
3
Can you post your routes.php?aceraven777
Try this route: Route::post('store', 'PostController@store')rad11

3 Answers

1
votes

The problem is here:

<form method="post" action="store">

You should put posts here:

<form method="post" action="posts">

You can see all routes created with Route::resource() by using php artisan route:list command. Here, you need to look at URI created for posts.store route.

Also, you need to add CSRF token to your form:

<form method="post" action="posts">
    {{ csrf_field() }}
1
votes

<form method="post" action="store"> will send you to the path store which you do not have, your form should post to the same url, like this:

<form method="post" action=".">
0
votes

use

<form method="post" action="posts">