我有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