How to paginate a related model in laravel

i have two model which is RoomCategory and Room. and i want to paginate all rooms related by each roomcategory by a foreach loop, here’s my 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()

    return View::make('pages.roomlist', compact('room_category'));


@foreach($room_category as $category)
    @foreach($category->room as $rooms)



and i got this error

Call to undefined method IlluminateDatabaseEloquentCollection::with()

can someone help me how to paginate those rooms under a foreach loop of the roomcategories. thanks

Source: laravel

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.