【发布时间】:2015-08-10 14:39:35
【问题描述】:
我有两个模型,即 RoomCategory 和 Room。我想通过foreach循环对每个房间类别相关的所有房间进行分页,这是我的MVC。
型号
class RoomCategory extends \Eloquent {
public function Room(){
return $this->hasMany('Room','category_id','id','RoomCategory');
}
}
class Room extends \Eloquent {
public function RoomCategory(){
return $this->belongsTo('RoomCategory','id','category_id','Room');
}
}
控制器
public function index()
{
$room_category = RoomCategory::get()
->with('Room')->paginate(1);
return View::make('pages.roomlist', compact('room_category'));
}
查看
@foreach($room_category as $category)
{{$category->name}}
@foreach($category->room as $rooms)
{{$rooms->name}}
{{$category->room->links()}}
@endforeach
@endforeach
我得到了这个错误
调用未定义的方法 Illuminate\Database\Eloquent\Collection::with()
有人可以帮助我如何在房间类别的 foreach 循环下对这些房间进行分页。谢谢
【问题讨论】:
标签: laravel laravel-4 model pagination eloquent