I'm using Laravel 4.1.x and I have a Has Many and Belongs To Many relationship between Tags and Photos (A photo belongs to many tags, a tag belongs to many photos). I'm using a pivot table with tag_id and photo_id
I know how to paginate the tags, using:
Tag::paginate($limit);
I'm able to fetch one tag with all the related photos using:
Tag::find($id);
But what I want to do now, is to retrieve all the photos that belongs to a tag , but paginating them. So, its something like the second code that I show, but with a page and limit parameter.
For example, I would want to see the first 15 photos that belongs to the tag with id=1, and be able to after that select the second 15.
Update:
Based on the answer provided, I tried with ->photos()->paginate(), but using this, it doesn't returns the rest of the informaton from the tag (Name, description, etc).
I've been able to get everything using:
$tag = Tag::findOrFail($id)->toArray();
$tag['photos'] = Tag::findOrFail($id)->photos()->paginate($limit)->toArray();
But I'm not sure if there is any other way to do this, and if this is generating an extra query.