1
votes

Edit3: Could a reason be because both controllers are leading to the same page?

Edit2: Still not working after the answers I got.

Edit: Error one is solved, now I'm getting:

Undefined variable: project (View: /var/www/resources/views/pages/showProject.blade.php)

Can this be because both variables are leading to the same page? The projects variable was working perfectly before the comment system.

public function index()
{
    $projects = Project::all();

    return view('pages.projects', compact('projects'));
}

Project variable declare.

I'm trying to get my comments from my database to show on a specific 'project page' in the laravel 5 project I'm working on. The idea is that the user can add art projects and other users can comment on them, but whenever I try to visit the page I get

Undefined variable: comments (View: /var/www/resources/views/pages/showProject.blade.php)

This is my controller

public function index()
{
  $comments = Comment::all();

  return view('pages.showProject', compact('comments'));
}

public function store()
  {
    $input = Request::all();
    $comment = new Comment;
    $comment->body = $input['body'];
    $comment->project_id = $input['project_id'];
    $comment->user_id = Auth::user()->id;
    $comment->save();
    return redirect('projects/'.$input['project_id']);

}

These are my routes

// add comment
Route::post('projects/{id}','CommentController@store');

// show comments
Route::post('projects/{id}','CommentController@index');

And my view

 @if (Auth::check())

    <article> <!--Add comment -->

        <br/>
        {!! Form::open() !!}
        {!! form::text('body', null, ['class' => 'form-control']) !!}

        <br/>

        {!! Form::Submit('Post Comment', ['class' => 'btn btn-primary form-control']) !!}
        {!! Form::hidden('project_id', $project->id) !!}


        {!! Form::close() !!}
        <br/>

    </article>

    <article>

        @foreach ($comments as $comment)

            <article>

                <p>Body: {{ $comment->body }}</p>
                <p>Author: {{ $comment->user->name }}</p>

            </article>


        @endforeach

    </article>
    @else
    <p>Please log in to comment</p>
   @endif

The Model

class Comment extends Model
{
    //comments table in database
    protected $guarded = [];

    // user who has commented
    public function author()
    {
        return $this->belongsTo('App\User','user_id');
    }

    // returns post of any comment
    public function post()
    {
        return $this->belongsTo('App\Project','project_id');
    }

    public function comments()
    {
        return $this->hasMany('App\Comment');
    }

    public $timestamps = false;
}

Is there any way I can solve this?

Thanks in advance

3
try return view('pages.showProject', array('comments'=>$comments));ka_lin
'Expression result unused' & 'unreachable statement' on the $comments part of the return view. I'll update the OP with my model.Joren Polfliet
Also Route::post('projects/{id}','CommentController@index'); should be Route::get('projects/','CommentController@index');//the id is not used and you are using GET not POSTka_lin
Ok, that was my bad. I replaced that and now the error says 'Undefined variable: project (View: /var/www/resources/views/pages/showProject.blade.php)'. Can this be because both the comments and projects variables are leading to the same page?Joren Polfliet
Add the project variable as an associative array in the view like for the comments (array('project'=>Project::where('id', '=', $id)->firstOrFail())) and in the controller get the $id from the request(function index($id), change route with the id in the url(Route::get('projects/{id}','CommentController@index');)ka_lin

3 Answers

1
votes

First, you need to make sure that you are aliasing your 'Comment' model in your controller. This is done with the use statement.

use App\Comment;

class CommentController extends Controller
{
    public function index()
    {
      $comments = Comment::all();

      return view('pages.showProject', compact('comments'));
    }
}

Second, you will need to change your route for showing comments from a POST request to a GET request. At the moment you are making identical routes and furthermore GET is the correct request type for retrieving data.

Route::get('projects/{id}','CommentController@index');

Third, you are referencing a $project variable in your view, but never passing it in from the controller. That needs to reference something.

0
votes

I think your relationship is wrong. Try this:

Comment model:

class Comment extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

User model:

class User extends Model
{
    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

In the view you can use:

@foreach($comments as $comment)
    <p>{{ $comment->user->name }}</p>
@endforeach
0
votes

I needed to empty the show in the commentsController and only use it for saving the comments. For showing them I made an extra function in my projectsController. It ended up being this;

 public function show($id)
    {
    $project = Project::findOrFail($id)->load("User");

    $input = Request::all();

    //-----------------------DB-----------------------------//
    $project_comments = DB::table('comments')
        ->select('body', 'name')
        ->where('project_id', '=', $id)
        ->join('users', 'users.id', '=', 'user_id')
        ->get();
    //-----------------------DB-----------------------------//

    return view('pages.showProject', ['project' => Project::findOrFail($id), 'comments' => $project_comments]);
}