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.

Model

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');
    }
}

Controller

public function index()
{
    $room_category = RoomCategory::get()
        ->with('Room')->paginate(1);

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

View

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

         {{$rooms->name}}

         {{$category->room->links()}}
    @endforeach
@endforeach

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