Hey guys,
I have an Event model which has many Status'. I am paginating the statuses and using render() in the view to generate page links. I'm also caching these statuses like so:
// $id is the event ID
$statuses = Cache::remember('statuses_' . $id, 3, function() use ($event, $sort) {
return $event->statuses()
->with('comments')
->latest()
->paginate(10);
});
However, this doesn't work as expected because the cache simply caches what it's given - and in this case it's given the first page of results. So when you navigate to the second page, you still get the first page of results.
Someone suggested not using paginate() and using skip() and take() to achieve the same effect:
// $id is the event ID
$page = $request->has('page') ? $request->query('page') : 1;
$statuses = Cache::remember('statuses_' . $id . '_page_' . $page, 3, function() use($event, $page) {
return $event->statuses()
->with('comments')
->latest()
->take(10)
->skip( ($page - 1) * 10)
->get();
});
However, this means I can't use the render() method to generate the pagination links in the view. I need the links for non-js users and for search engines to crawl the pages.
I saw this code on github that caches pagination but have no idea where to start with it and not sure if it works with L5. - https://github.com/fideloper/Implementing-Laravel
If anyone has any pointers/suggestions that would be great!
Thanks