krex's avatar
Level 4

Cache forever using tags and pagination

Hi,

I want to Cache forever using cache tags like this:

Cache::tags('news')->rememberForever('allposts', function(){ return Article::with('image', 'category')->IsPublished()->Published()->latest('published_at')->paginate(8); });

But in the cached result there are only first 8 records and pagination doesn't work. How to cache all of the posts and then use pagination?

Thanks, Best regards

0 likes
1 reply
krex's avatar
Level 4

Soo i've decided to do this: My Cotroller:

public function index(Request $request, Ourlife $ourlife)
{

    if($request->get('page') == null || $request->get('page') == '1') {
        $page = 1;
    } else {
       $page = $request->get('page');
    }
}

$articles = $this->newsRepository->getNews($page);

and my cache Repository :

public function getNews($page) {
    if(Cache::tags('news')->get('articles-'.$page) == null){
        $articles =  Cache::tags('news')->rememberForever('articles-'.$page, function(){
          return Article::with('image', 'category')->IsPublished()->Published()->latest('published_at')->paginate(8);
                        });

        return $articles;
         } else {

        $articles = Cache::tags('news')->get('articles-'.$page);

    }
    return $articles;
}

Is there a better way?

Please or to participate in this conversation.