我在使用laravel将我的表单发布到我的数据库时遇到了问题。当我单击submit时,它在RouteCollection.php第218行显示错误MethodNotAllowedHttpException。我的HTML代码如下所示。我已经定义了如下所示的路由,并且我还粘贴了包含存储函数的PostController。
<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>
这是我的路线页面
Route::resource('posts', 'PostController');
这是包含store函数的PostController,该函数用于将数据存储到数据库中。
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);
}
发布于 2016-12-08 07:17:18
问题出在这里:
<form method="post" action="store">
你应该把posts
放在这里:
<form method="post" action="posts">
您可以使用php artisan route:list
命令查看使用Route::resource()
创建的所有路由。这里,您需要查看为posts.store
路由创建的URI。
此外,您还需要在表单中添加CSRF token:
<form method="post" action="posts">
{{ csrf_field() }}
发布于 2016-12-08 07:17:23
<form method="post" action="store">
会将您发送到您没有的路径store
,您的表单应该发送到相同的url,如下所示:
<form method="post" action=".">
发布于 2016-12-08 09:57:21
使用
<form method="post" action="posts">
https://stackoverflow.com/questions/41033785
复制