2
votes

fellow coders.

Whenever I try to POST something, I get this error.

enter image description here

create.blade.php file:

    <h1>Publish a Post</h1>

    <hr>

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

      {{ csrf_field() }}

      <div class="form-group">
        <label for="title">Title</label>
        <input type="text" class="form-control" id="title" name="title">
      </div>

      <div class="form-group">
        <label for="body">Body</label>
        <textarea type="text" class="form-control" id="body" name="body"></textarea>
      </div>

      <button type="submit" class="btn btn-primary">Publish</button>

    </form>

  </div>

web.php file:

Route::get('/', 'PostController@index');

Route::get('/posts/create', 'PostController@create');

Route::get('/posts', 'PostController@store');

PostController.php file:

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

    public function store()
    {
      dd(request()->all());
    }

And the database schema:

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->text('body');
        $table->timestamps();
    });
}

Any ideas what am I doing wrong or how to fix this issue would be really appreciated.

2

2 Answers

3
votes

You have no POST defined in your routes. Update your ::get to ::post for your endpoint.

Route::post('/posts', 'PostController@store');

For more information: https://laravel.com/docs/5.6/routing#basic-routing

0
votes

you may register a resourceful route to the controller:

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

For more information: https://laravel.com/docs/5.6/controllers