0

我正在尝试向我的 laravel 应用程序添加评论系统。但我似乎无法让它工作。我有两个模型

class Post extends \Eloquent {

protected $table = 'posts';

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

和我的评论模型

class Comment extends \Eloquent {

protected $table = 'comments';

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

}

在我的仪表板控制器中,我试图从模型中获取输出

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

我认为我的数据库已正确链接,但现在我收到错误“找不到类注释”。对这个有什么建议吗?

4

1 回答 1

3

首先试试这个:composer dump-auto(由 user1669496 评论)

如果这没有帮助,那么改变你的模型......

改变这个:

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

像这样:

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

对 Post 模型执行类似操作。

只需将 App\Models\XXXX 更改为保存 Post 模型的命名空间。

我有类似的问题,这对我有帮助,希望对您有所帮助。

于 2013-11-01T17:52:23.467 回答