0

I'm just trying to insert title and body using eloquent on database and MethodNotAllowedHttpException appears wont let me do it cause of that error

Here's the controller

public function store(Request $request)
    {

        $this->validate($request, array(
                'title' => 'required|max:255',
                'body' => 'required'
            ));

        $post = new Post; 

        $post->title = $request->title;
        $post->body = $request->body;

        $post->save();

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

    }

Here's my view create.blade.php

<form action="post.store" method="POST">
                <div class="form-group">
                    <label for="title">Title</label>
                    <input type="text" class="form-control" name="title" placeholder="Enter title">
                </div>
                <div class="form-group">
                    <label for="body">Body</label>
                    <textarea class="form-control" name="body" rows="3"></textarea>
                </div>
                <div class="form-group">

                    <input type="submit" class="btn btn-success btn pull-right" value="post">
                </div>
</form>
Lestah
  • 63
  • 1
  • 2
  • 9

3 Answers3

3

i am assuming your are using resource route.

i think this <form action="post.store" method="POST"> should be <form action="{{ route('post.store') }}" method="POST"> and use this too {{ csrf_field() }} inside form otherwise you will get tokenmismatch error.

<form action="{{ route('posts.store') }}" method="POST">
{{ csrf_field() }}
                <div class="form-group">
                    <label for="title">Title</label>
                    <input type="text" class="form-control" name="title" placeholder="Enter title">
                </div>
                <div class="form-group">
                    <label for="body">Body</label>
                    <textarea class="form-control" name="body" rows="3"></textarea>
                </div>
                <div class="form-group">

                    <input type="submit" class="btn btn-success btn pull-right" value="post">
                </div>
</form>
Riaz Laskar
  • 1,317
  • 9
  • 17
  • yes your right im using resource on route i've tried {{ route('post.store') }} and put {{ csrf_field() }} inside form and i got a new error ErrorException Route [post.store] not defined. – Lestah Jun 23 '17 at 03:51
  • in console use the command `php artisan route:list` and check weather `post.store` is correct or not – Riaz Laskar Jun 23 '17 at 03:57
  • but here its `
    ` just update it `
    ` it should work fine.
    – Riaz Laskar Jun 23 '17 at 05:34
  • After you have made a change in your `web.php`, it is good to always do a `php artisan route:cache` to refresh the routes. See if you are still facing `[post.store]` error? – Ru Chern Chong Jun 23 '17 at 14:17
0

You may use command php artisan route:list and in column Name you will see route name and you will be able to call in view by {{ route('routename') }}

Example: In route list is shown posts.store, so you will call in view {{ route('posts.store') }}

I think you may call incorrect route by above route view is posts.show but when insert you called post.store maybe posts.store

Sovary
  • 692
  • 4
  • 11
  • i always use php artisan route:list on my terminal to check the route names im sure of posts.store im looking rightnow on my list of routes – Lestah Jun 23 '17 at 05:27
  • but I saw your create.blade.php is `post.store` not `posts.store` as your mention. should use with double curly bracket `{{ route('posts.store') }}` – Sovary Jun 23 '17 at 05:44
0

use in form action {{ route('posts.store') }}

Vishal Varshney
  • 905
  • 6
  • 11