Search code examples
phplaravelformshttp-redirectstore

Laravel my post method (store) form not working


PostsController.php

 public function store(Request $request)
    {
 Post::create($request->all());
 return redirect('/posts');
}

my view create.blade.php in posts directory

@extends('layout.app')



@section('content')

    <h1>Create Post</h1>

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

        <input type="text" name="title" placeholder="Enter title"> <!-- this name=title comes from create_posts_table -->
        {{--{{csrf_field()}}--}}
        <input type="submit" name="submit">

    </form>

@stop

Route

Route::resource('/posts','PostsController');

when I submit browser goes to localhost/posts and it says:

Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

Error 404
localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.5

and no record wont save


Solution

  • try :

    <form action="{{ route('posts.store') }}" method="post">
        //
    </form>
    

    or you can read this document for more information. and take note you CAN NOT send post request by browser calling url. you can submit a form for it