Laravel 4 拉雷维尔的雄辩故事
如果我想找到有评论的帖子,就像Laravel 4 拉雷维尔的雄辩故事,laravel-4,Laravel 4,如果我想找到有评论的帖子,就像 Post::with('comments')->has('comments'); 在我的例子中,我有一个评论表,每个评论只有一个回复,因此,结构是 评论表 id name comment_id 评论模式 public function reply() { return $this->hasOne('comment', 'comment_id'); } 如果我想找到主题 Comment::whereNull('comment_id');
Post::with('comments')->has('comments');
在我的例子中,我有一个评论表,每个评论只有一个回复,因此,结构是
评论表
id
name
comment_id
评论模式
public function reply() {
return $this->hasOne('comment', 'comment_id');
}
如果我想找到主题
Comment::whereNull('comment_id');
但如果我想知道,评论已经回复了
Comment::whereNull('comment_id')->has('reply');
它将是空的,如何通过此结构找到评论有和没有回复您在您的关系中缺少了
返回
:
public function reply() {
return $this->hasOne('Comment');
}
您可能还发现需要定义外键:
public function reply() {
return $this->hasOne('Comment', 'comment_id');
}
以下是供您参考的文件:
编辑:
您可能会发现需要使用hasMany
关系而不是hasOne
:
public function reply() {
return $this->hasMany('Comment', 'comment_id');
}
您可能需要使用外键尝试
hasMany
。请参见编辑。