使用laravel4中的关系访问表中的数据

时间:2014-02-14 09:37:11

标签: laravel-4

我有这些表格 用户:ID名称 的帖子:POST_ID,张贴,USER_ID 的评论:COMMENT_ID,注释,POST_ID

我在maodel中的关系是

用户模型

public function posts()
 {
  return $this->hasMany('Post','post_id');
   }

发布模型

public function user()
{
return $this->belongsTo('User','post_id');
}

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

评论模型

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

我正在使用

访问帖子的评论
 $comments = Post::find(1)->comments;  But its giving an error. 

请指导我。 感谢。

1 个答案:

答案 0 :(得分:0)

尝试

$comments = Post::find(1)->comments();

你没有打电话给你的功能,你忘了括号。

相关问题