Php Laravel我的系统的一对多关系

Php Laravel我的系统的一对多关系,php,laravel,Php,Laravel,我想根据我的系统中的一家酒店输出所有评论。目前,我有一个系统,显示每个酒店的所有评论。我有以下代码 PostsController: public function show($id) { $post = Post::find($id); $review = Review::all(); return view('posts.show', compact('post', 'review')); } Posts.php: protected $tables='po

我想根据我的系统中的一家酒店输出所有评论。目前,我有一个系统,显示每个酒店的所有评论。我有以下代码

PostsController:

public function show($id)
 {
   $post = Post::find($id);
   $review = Review::all();
   return view('posts.show', compact('post', 'review')); 
 }
Posts.php:

    protected $tables='posts';
    function review()
      {
        return $this->hasMany('App\Review');
      }
     }
Review.php:

     protected $tables='reviews';
     function post()
       {
         return $this->hasMany('App\Post', 'posts_title', 'title');
       }

我想返回正确酒店的匹配评论。我想返回posts\u title(posts表中的主列)和title(reviews表中的列)。

根据
Review
模型中的
post
函数检查文件名
posts.php
。我不知道你的数据库结构是什么样子,但如果你对
主键
使用
Laravel
最佳实践
id
,对
外键
使用
post\u id
会更好,而且我认为这种关系应该是一对多的,这意味着一篇文章有很多这样的评论:
Post.php

public function reviews(){
  return $this->hasMany('App\Review');
}
public function post(){
  return $this->belongsTo('App\Post');
}
Review.php

public function reviews(){
  return $this->hasMany('App\Review');
}
public function post(){
  return $this->belongsTo('App\Post');
}
然后你可以像这样使用它

$post = Post::with('reviews')->whereId($id)->first();

要获得更多信息,您可以访问

hi,我已更改了代码以匹配该代码,但我现在收到以下错误:未找到列:1054未知列“reviews.post_id”位于“where子句”(SQL:select*from
reviews
where
reviews
post id
in(2))。我没有一个名为post_ID的列,这很奇怪,我还需要同时使用“title”和“post_title”,因为我是基于标题本身的信息。不幸的是,使用ID无法解决我的问题这是否回答了你的问题?你能为帖子和评论提供数据库表设计吗?管理说明-请不要发布双重/重复问题。编辑或删除原始问题。