Question

Im trying to add a comment system to my laravel app. But I can't seem to get it working. I have two models

class Post extends \Eloquent {

protected $table = 'posts';

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

and my Comment model

class Comment extends \Eloquent {

protected $table = 'comments';

public function post()
{
    return $this->belongsTo('Post');
}

}

in my DashBoardController I'm trying to get the output from the models

use App\Models\Post;
use App\Models\Comment;
use Input, Redirect, Sentry, Str, View, Notification;

class DashboardController extends \BaseController {
public function index()
{
    $post = Post::find(3)->comments()->comment;
    print_r($post);die;
}  
}

I think my database is properly linked, but now I'm getting the error 'Class Comment not found'. Any advice on this one?

Was it helpful?

Solution

First try this: composer dump-auto (as commented by user1669496)

if this didn't helped then change your model...

Change this:

    return $this->belongsTo('Post');

to smth like this:

    $this->belongsTo('App\Models\Post');

Do the similar for Post model.

Just change App\Models\XXXX to your namespace where you have Post model saved.

I had similar problem and this helped me, hope it will help you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top