amk's avatar
Level 4

Cache and Pagination

$page = $request->has('page') ? $request->query('page') : 1;

 $user = Cache::rememberForever('user_'.$page, function () use ($course_id) {
      return User::where('course_id',$course_id)->pagination('5');
  });

I wanna know if a user create or edit,how do I forget cache?

0 likes
6 replies
jlrdw's avatar

With pagination being very efficient anyway, why even bother with cache for this.

Usually you use cache for data that doesn't change that often.

2 likes
Robstar's avatar

@amk The method is paginate to pagination i.e.

return User::where('course_id',$course_id)->paginate(5)

So your code would become:

$page = $request->query('page', 1);

$user = cache()->rememberForever("user.{$page}", function () use ($id) {
    return User::where('course_id', $id)->paginate(5);
});

amk's avatar
Level 4

@jlrdw So,you mean I shouldn't use Cache if data is usually changed?

amk's avatar
Level 4

@robstar thank you Sir... If I create new user,how to delete old cache?

munazzil's avatar

Just Type in your cmd.

php artisan cache:clear

Please or to participate in this conversation.