Himanshu Rahi
Himanshu Rahi

Reputation: 301

Getting MethodNotAllowedHttpException No message Error While Storing the data into database [Laravel]

Hi am new in laravel and am trying to store the data into database and get this error:

MethodNotAllowedHttpException No message

here i created the resources

Routes:
Route::resource("/post","PostController");

Here is the store method in my PostController

public function store(Request $request)
{


    Post::create($request->all());
    return redirect('post');

}

Here is my HTML form for post

<!DOCTYPE html>
<html>
<head>
<title>Create Post</title>
</head>
<body>

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

    <input type="text" name="title" placeholder="Enter Title"><br>
    <input type="submit" name="submit">

</form>
</body>
</html>

Post Model

<?php

 namespace App;

 use Illuminate\Database\Eloquent\Model;

 class Post extends Model
 {
 protected $fillable = ["_token","title"];
 protected $table = 'Post';
  }

here is my route list:

Route List

Here is full error Error

Upvotes: 0

Views: 211

Answers (3)

Prathamesh Doke
Prathamesh Doke

Reputation: 845

You have to send the data to the view.

public function store(Request $request)
{
    $data = Post::create($request->all());
    return redirect('post')->with('data',$data);
}

with the following route->

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

Upvotes: 0

George Hanson
George Hanson

Reputation: 3040

Specify the action in the form.

You can update the HTML to this and it should work:

<!DOCTYPE html>
<html>
<head>
<title>Create Post</title>
</head>
<body>

<form action="{{ url('/post') }}" method="post">
    {{  csrf_field() }}

    <input type="text" name="title" placeholder="Enter Title"><br>
    <input type="submit" name="submit">

</form>
</body>
</html>

This is because the create route is /post/create and the store route is /post. Because no action is specified, the form will post to /post/create instead which is why the exception is being thrown.

Upvotes: 0

CodeBoyCode
CodeBoyCode

Reputation: 2267

You need to add a form action here, post.store should work considering that you are using a resource controller':

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

If this doesn't help let me know.

Upvotes: 2

Related Questions