2016-12-08 96 views
1

我無法使用laravel將我的表單發佈到我的數據庫。當我點擊提交時,它顯示了RouteCollection.php第218.行中的錯誤MethodNotAllowedHttpException。我的HTML代碼如下所示。我已經定義瞭如下所示的路線,並且我也粘貼了包含商店功能的PostController。RouteCollection.php中的MethodNotAllowedHttpException行218:Laravel

<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'); 

這是一個包含了是假設將數據存儲到數據庫中的存儲功能的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); 
     } 
+0

你能發表您的'''routes.php文件'''? – aceraven777

+0

試試這條路線:Route :: post('store','PostController @ store') – rad11

回答

0

的問題是在這裏:

<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() }} 
1

<form method="post" action="store">會送你到你沒有路徑store,你的表格應張貼到相同的URL,就像這樣:

<form method="post" action="."> 
+0

嗨,非常感謝你的工作。請問拉拉維爾如何知道它應該調用我的商店函數以便發佈到數據庫?如果我想讓laravel執行另一個名爲processInformation的進程函數呢?我該怎麼把這個動作當作?謝謝 – Desmond

+0

請參閱[docs](https://laravel.com/docs/5.3/controllers#resource-controllers)。基本上通過使用Route :: resource('posts','PostController'),它會生成文檔中表格中提到的所有路由。如果您想添加另一個單一路線,例如processInformation,請遵循[Basic Controllers](https://laravel.com/docs/5.3/controllers#basic-controllers)中的步驟,例如'Route :: get('posts/processInformation','PostController @ processInformation')'並放在你的Route :: resource(...) – Unnawut

0

使用

<form method="post" action="posts"> 
相關問題