2014-04-23 57 views
1

我正在做一個網站,您可以上傳代碼(codesnippet)。我希望人們可以評論用戶上傳的代碼。我想爲每個上傳的代碼添加一個新線程。但我有一個問題繼承人是我的代碼 形式:Laravel評論系統的具體「發佈」

{{ Form::open(array('url'=>'comments')) }} 
@if (Auth::check()) 
    {{ Form::hidden('codesnippet_id' -> $codesnipp_id)) }} 
    {{ Form::textarea('comment', null, array('placeholder'=>'comment')) }} 
    {{ Form::submit('comment', array('class'=>'comment-knapp'))}} 
    {{ Form::close() }} 
    @endif 


@foreach($comments as $comment) 
     {{ $comment->created_at }}<br> 
     {{ $comment->comment }}<br><br> 
@endforeach 

路線:

Route::post('comments/{id?}', array('uses' => '[email protected]')); 

Route::get('comments/{id?}', array('uses' => '[email protected]')); 

型號:

<?php 


class Comment extends Eloquent { 

    public function getComments($codesnipp_id){ 
     $getComments = DB::table('comments') 
         ->where('codesnippet_id', '=', $codesnipp_id) 
         ->select('id', 'comment', 'votes', 'user_id', 'created_at', 'codesnippet_id') 
         ->get(); 
     return $getComments; 
    } 

    public function writeComment($userID, $comment, $codesnipp_id){ 
     DB::table('comments') 
     ->insert(array('user_id' => $userID, 
         'comment' => $comment, 
         'codesnippet_id' => $codesnipp_id)); 
    } 
} 

控制器:

<?php 

class CommentController extends BaseController { 

    public $restful = true; 
    public $CM; 


    function __construct(){ 
     $this->CM = new Comment(); 
    } 

    public function newComment($codesnipp_id){ 
     $comment = Input::get('comment'); 

     $rules = array('comment'=>'required|alphaNum|min:1|max:255'); 

     $validator = Validator::make(Input::all(), $rules); 

     if ($validator->fails()) { 
      return 'Du måste vara inloggad för att kunna kommentera! 
        <a href="#loginmodal" id="modaltrigger">Klicka här</a> för att logga in'; 
     } else { 
      // $comment = Input::get('comment'); 
      $username = Session::get('user'); 
      echo $username . '<br><br><br>'; 
      $userID = $this->get_id_by_string('users','username' , $username); 
      return $this->CM->writeComment($userID, $comment, $codesnipp_id); 

      // return Redirect::to('comment')->with('message', 'Något gick fel')->withErrors($validator)->withInput(); 
     } 
    } 

    public function getComments($codesnipp_id) 
    { 
     $comments = $this->CM->getComments($codesnipp_id); 
     return $comments; 
    } 
} 

的問題是,我得到一個錯誤Missin g參數1與我的控制器中的$ codesnipp_id。 順便說一句,在我的評論表中我有一個codesnippet_id連接到codesnippet表id。 我想我錯過了一些東西,我需要幫助! Ps。對不起英文不好。

回答

1

改變這條路線:

Route::post('comments/{id?}', array('uses' => '[email protected]')); 
//to 
Route::post('comments/{id}', array('uses' => '[email protected]')); 

作爲該方法需要id參數。

然後改變形式開放:

{{ Form::open(array('url'=>'comments/'.$codesnipp_id)) }} 
// or better to this: 
{{ Form::open(array('action'=>array('[email protected]', $codesnipp_id))) }}