我想在页面中的post_id(post1)下检索所有评论:
//localhost/posts/post1/
表评论
id:int[primary], comment:varchar, post_id:varchar, comment_id:varchar
1 this is a comment post_1 comment_1
表格帖子
id:int[primary], post_title:varchar, post_id:varchar
1 this is post title post_1
模型comment.php
public function post()
{
return $This->belongsTo('Post');
}
model post.php
public function comments()
{
return $this->hasMany('Comment');
}
controller postsController.php
public function show($id)
{
$comments = Post::where('post_id','=',$id)->first()
->comments()->where('post_id','=',$id)->get();
}
当我访问// localhost / posts / post1 /时,不会显示相关评论。 SQL运行如下:
select * from `posts` where `post_id` = 'post1' limit 1
select * from `comments` where `comments`.`post_id` = '1' and `post_id` = 'post1'
如何删除`post_id
='1'以检索相应的评论?
答案 0 :(得分:0)
在你的帖子表中,id列应该被命名为id而不是post_id,因为这是laravel所期望的。可以将您的雄辩模型中的主键设置为id以外的其他键,但最好使用id并遵循标准。请参阅此处的文档http://four.laravel.com/docs/eloquent。一旦将id列命名为id,就可以通过Post :: find($ id)
获取单个帖子