我有users
有多個restaurants
,那麼每個restaurant
有comments
。從多個相關記錄laravel中獲取一組相關記錄?
因此,當user
登錄時,我想顯示與用戶相關的所有comments
與restaurants
相關的列表。
我使用laravel 5.1
我有什麼到目前爲止是:
$restaurants = $user->restaurants;
$comments = null;
foreach ($restaurants as $restaurant) {
if (!$comments){
$comments = $restaurants->comments();
} else{
$comments = $comments->merge($restaurant->comments());
}
}
$rows = $comments->paginate(15);
$count = $comments->count();
我得到一個BadMethodCallException
,我覺得我沒有做這樣的laravel方式。
嗯,似乎成爲一個問題,'用戶'到'餐廳'是一個'多對多'的關係...... – surfer190