Laravel特定评论系统" post"

时间:2014-04-23 14:24:48

标签: php laravel comments

我正在建立一个可以上传代码(codesnippet)的网站。我希望人们可以评论用户上传的代码。我想为每个上传的代码创建一个新线程。但我有一个问题是继承人是我的代码 形式:

{{ 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。 顺便说一句,在我的评论表中,我有一个codenippet_id,它连接到codesnippet表id。 我想我错过了什么,我需要帮助! PS。抱歉英语不好。

1 个答案:

答案 0 :(得分:1)

更改此路线:

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

因为该方法需要id参数。

然后将表格开头更改为:

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