2015-10-16 17 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

回答

相關問題