我是laravel的新手,并尝试创建一个简单的博客应用程序,我想将帖子详细信息存储到数据库中。我也使用laravel命令设置我的数据库和表。 我创建了一个以下表单,以便将数据存储到数据库中:
@extends('main')
@section('title','| Create Post')
@endsection
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Create New Post</h1>
<hr/>
<form action="store" method="POST">
<div class="form-group">
<div class="form-group">
<label name="title">Title:</label>
<input id="title" name="title" class="form-control">
</div>
<div class="form-group">
<label name="body">Post Body:</label>
<textarea id="body" name="body" class="form-control"></textarea>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="POST">
<input type="submit" class="btn btn-success btn-lg btn-block" value="Save Post">
</div>
</form>
</div>
</div>
@endsection
我为此表单提交请求创建了一个路由规则,如下所示:
Route::resource('posts','PostController');
其中PostController是控制器,其中实现了所有必需的资源方法,用于处理和保存数据
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller {
public function index()
{
//
}
public function create()
{
return view('posts.create');
}
public function store(Request $request)
{
//validate the data
$this->validate($request,array(
'title' => 'required|max:255',
'body' => 'required'
));
//store in the database
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->save();
//redirect to page
return redirect()->route('posts.show',$post->id);
}
public function show($id)
{
//
}
public function edit($id)
{
//
}
public function update($id)
{
//
}
public function destroy($id)
{
//
}
}
我还使用命令提示符列出了我的路线,它提供了以下输出:
App \ Http \ Controllers \ PagesController @ getIndex | | | | GET | HEAD |帖子| posts.index | App \ Http \ Controllers \ PostController @ index | | | | GET | HEAD |发布/创建| posts.create | App \ Http \ Controllers \ PostController @ create | | | | POST |帖子| posts.store | App \ Http \ Controllers \ PostController @ store | | | | GET | HEAD |帖子/ {posts} | posts.show | App \ Http \ Controllers \ PostController @ show | | | | GET | HEAD |帖子/ {posts} / edit | posts.edit | App \ Http \ Controllers \ PostController @ edit | | | | PUT |帖子/ {posts} | posts.update | App \ Http \ Controllers \ PostController @ update | | | |补丁|帖子/ {posts} | | App \ Http \ Controllers \ PostController @ update | | | |删除|帖子/ {posts} | posts.destroy | App \ Http \ Controllers \ PostController @ destroy
我在提交保存详细信息的表单后收到MethodNotAllowedHttpException。
答案 0 :(得分:0)
试试这个:
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>Create New Post</h1>
<hr/>
<form action="{{ route('posts.store') }}" method="POST">
<div class="form-group">
<div class="form-group">
<label name="title">Title:</label>
<input id="title" name="title" class="form-control">
</div>
<div class="form-group">
<label name="body">Post Body:</label>
<textarea id="body" name="body" class="form-control"></textarea>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="_method" value="POST">
<input type="submit" class="btn btn-success btn-lg btn-block" value="Save Post">
</div>
</form>
</div>
</div>
@endsection