Forget Cache on Save

Tip given by @pratiksh404

If you have cache key like posts that gives collection, and you want to forget that cache key on new store or update, you can call static saved function on your model:

1class Post extends Model
2{
3 // Forget cache key on storing or updating
4 public static function boot()
5 {
6 parent::boot();
7 static::saved(function () {
8 Cache::forget('posts');
9 });
10 }
11}

Like our articles?

Become a Premium Member for $129/year or $29/month
What else you will get:
  • 22 courses (477 lessons, total 38 h 20 min)
  • 2 long-form tutorials (one new every week)
  • access to project repositories
  • access to private Discord