H
H
Harconnen2015-05-19 14:38:51
Laravel
Harconnen, 2015-05-19 14:38:51

How to cache paginated output in Laravel 5?

Hello!
Maybe someone came across Laravel 5 with pagination caching.
There were no problems in version 4, I used:

->remember(60)
->paginate(10);

- everything worked. In version 5, it does not want to work like this, but with this option:
$value = Cache::remember('users', $minutes, function()
{
    return DB::table('users')->paginate(15);
});

shows the same data on all pages.
Everything works without cache.
Sincerely, Konstantin.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Plisko, 2015-05-19
@Harconnen

Of course, it will not work, your cache key does not take into account the page number. You need something like (I did not work with the fifth version and there is no way to check the code at hand)

$value = Cache::remember('users-' .  \Request::input('page'), $minutes, function()
{
    return DB::table('users')->paginate(15);
});

And why are you licking for data not through ORM, but through a dirty DB::table?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question