2015-10-16 47 views
1

我正在使用laravel在我的应用程序中实现聊天功能。我有这些模型:如何在laravel模型中获取forienkeys数据

`Users: id, name ` 
`Pages: id, user_id(foreign_key from Users table (id)), name` 
`Posts: id, page_id(foreign_key from Pages table (id)), user_id(foreign_key from Users table (id)), body` 
`Comments: id, post_id(foreign_key from Posts table (id)), user_id(foreign_key from Users table (id)), body` 

我需要查询以下

获取意见和与用户

获取页面的所有文章与评论,并与用户

的用户的所有帖子

我正在使用下面的查询来获取页面的所有文章,但我怎么能包括它的评论和用户的详细信息;

Page::with('post')->findOrFail($pageId)->post;

EDIT


USER Model: User.php 

public function page() { 
    return $this->hasMany('Page'); 
} 

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

Page Model: Page.php 

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

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

Post Model: Post.php 

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

public function page() { 
    return $this->belongsTo('Page'); 
} 

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

Comment Model: Comment.php 

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

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

请显示模型,以便我们可以看到您定义的关系 –

+0

我编辑了我的问题以包含模型 – anilCSE

回答

相关问题