2016-11-14 25 views
0

我是laravel的新手,嘗試創建一個簡單的應用程序,即博客,我想將一個帖子的詳細信息存入數據庫。我已經使用laravel命令設置了我的數據庫和表。MethodNotAllowedHttpException同時提交表單以使用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 | 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 | App \ Http \ Controllers \ PostController @ edit | | | | PUT |帖子/ {posts} | posts.update | App \ Http \ Controllers \ PostController @ update | | | | PATCH |帖子/ {posts} | | App \ Http \ Controllers \ PostController @ update | | | |刪除|帖子/ {posts} | posts.destroy | App \ Http \ Controllers \ PostController @ destroy

我在提交表單以保存細節後收到MethodNotAllowedHttpException。

+0

你的輸出告訴我,你的'''create'''方法接受'''POST'''其中存儲方法預計'''GET' '' –

回答

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 

Docs

+0

是的,謝謝先生,有用的答案和文檔。 –

+0

高興地幫助:) –

相關問題