user3565001
user3565001

Reputation: 11

Laravel commenting system for specific "post"

i am doing a site where you can upload code(codesnippet). And i want so that people can comment on the code the user uploads. And i want a new thread for every code that uploads. But i have a problem heres is my code Form:

{{ 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

Routes:

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

Route::get('comments/{id?}', array('uses' => 'CommentController@getComments'));

Model:

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

Controller:

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

The problem is that i get an error Missing argument 1 with $codesnipp_id in my controllers. By the way in my comments table i have an codesnippet_id that is connected to the codesnippet table id. I think i am missing something and i need help! Ps. sorry for the bad english.

Upvotes: 1

Views: 1143

Answers (1)

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81177

Change this route:

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

As that method requires id parameter.

Then change Form opening to:

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

Upvotes: 1

Related Questions