Laravel评论系统;帖子”;


Laravel commenting system for specific "post"

我正在做一个可以上传代码(代码片段)的网站。我希望人们可以对用户上传的代码发表评论。我希望每个上传的代码都有一个新的线程。但我有一个问题,这是我的代码形式:

{{ 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' => 'CommentController@newComment'));
Route::get('comments/{id?}', array('uses' => 'CommentController@getComments'));

型号:

<?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;
    }
}

问题是,我得到了一个错误,在我的控制器中缺少带有$codesnipp_id的参数1。顺便说一句,在我的注释表中,我有一个codesnippet_id,它连接到代码片段表id。我想我错过了什么,我需要帮助!抱歉英语不好。

更改此路由:

Route::post('comments/{id?}', array('uses' => 'CommentController@newComment'));
//to
Route::post('comments/{id}', array('uses' => 'CommentController@newComment'));

由于该方法需要id参数。

然后将Form opening更改为:

{{ Form::open(array('url'=>'comments/'.$codesnipp_id)) }}
// or better to this:
{{ Form::open(array('action'=>array('CommentController@newComment', $codesnipp_id))) }}