PHPackages                             mike-bronner/laravel-model-caching - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. mike-bronner/laravel-model-caching

ActiveLibrary[Caching](/categories/caching)

mike-bronner/laravel-model-caching
==================================

Automatic caching for Eloquent models.

13.1.1(1mo ago)2.4k2.0k↑854%2291MITPHPPHP &gt;=8.2CI passing

Since Sep 23Pushed 4d ago37 watchersCompare

[ Source](https://github.com/mike-bronner/laravel-model-caching)[ Packagist](https://packagist.org/packages/mike-bronner/laravel-model-caching)[ RSS](/packages/mike-bronner-laravel-model-caching/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (22)Versions (174)Used By (1)

🚀 Model Caching for Laravel
===========================

[](#-model-caching-for-laravel)

[![Laravel Package](https://github.com/mikebronner/laravel-model-caching/workflows/Laravel%20Package/badge.svg?branch=master)](https://github.com/mikebronner/laravel-model-caching/actions?query=workflow%3A%22Laravel+Package%22)[![Packagist](https://camo.githubusercontent.com/44978e2b054ec3165284b05886ef02f198abad3d18e0b36958cca1bb240c04f9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f47656e65614c6162732f6c61726176656c2d6d6f64656c2d63616368696e672e737667)](https://packagist.org/packages/genealabs/laravel-model-caching)[![GitHub license](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://raw.githubusercontent.com/mikebronner/laravel-model-caching/master/LICENSE)[![PHP Version](https://camo.githubusercontent.com/be1af5e2d4220e91c80f08d66824921cf467faf238ddba7b73e34b68cdf6b3b2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6d696b6562726f6e6e65722f6c61726176656c2d6d6f64656c2d63616368696e67)](https://packagist.org/packages/mikebronner/laravel-model-caching)[![Laravel](https://camo.githubusercontent.com/751230d25052d87343977f7ce2d80c65e1b67ffc7c041857d28434849f583755/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c61726176656c2d3131253230253743253230313225323025374325323031332d464632443230)](https://laravel.com)[![Latest Stable Version](https://camo.githubusercontent.com/35b000d2608a3f96c7215d380a3fbd48e7096f37bcb0426fa79330f5093367b8/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d696b6562726f6e6e65722f6c61726176656c2d6d6f64656c2d63616368696e67)](https://packagist.org/packages/mikebronner/laravel-model-caching)[![GitHub Stars](https://camo.githubusercontent.com/ea2e5933058d38adac976516974adca2ac415347c8858de34307ca010a61b30b/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6d696b6562726f6e6e65722f6c61726176656c2d6d6f64656c2d63616368696e67)](https://github.com/mikebronner/laravel-model-caching/stargazers)[![codecov](https://camo.githubusercontent.com/7b97b8faabfa298ea37de927d39fc201630203724701dd80d25c02cc9661eec3/68747470733a2f2f636f6465636f762e696f2f67682f6d696b6562726f6e6e65722f6c61726176656c2d6d6f64656c2d63616368696e672f67726170682f62616467652e7376673f746f6b656e3d41436b314b6b344f4c4f)](https://codecov.io/gh/mikebronner/laravel-model-caching)[![Tests](https://camo.githubusercontent.com/bff8028c81c2f026d08c74b9217f3e1def21dde4d972b536057516ffdd866d3e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74657374732d3333352532422d627269676874677265656e)](https://github.com/mikebronner/laravel-model-caching/tree/master/tests)

[![Model Caching for Laravel masthead image](https://repository-images.githubusercontent.com/103836049/b0d89480-f1b1-11e9-8e13-a7055f008fe6)](https://repository-images.githubusercontent.com/103836049/b0d89480-f1b1-11e9-8e13-a7055f008fe6)

🗂️ Table of Contents
--------------------

[](#️-table-of-contents)

- [📖 Summary](#-summary)
- [📦 Installation](#-installation)
- [🚀 Getting Started](#-getting-started)
- [⚙️ Configuration](#%EF%B8%8F-configuration)
- [🤝 Contributing](#-contributing)
- [⬆️ Upgrading](#%EF%B8%8F-upgrading)
- [🔐 Security](#-security)
- [📚 Further Reading](#-further-reading)

📖 Summary
---------

[](#-summary)

Automatic, self-invalidating Eloquent model and relationship caching. Add a trait to your models and all query results are cached automatically — no manual cache keys, no forgetting to invalidate. When a model is created, updated, or deleted the relevant cache entries are flushed for you.

⚡ Typical performance improvements range from 100–900% reduction in database queries on read-heavy pages. 🧪 Backed by 335+ integration tests across PHP 8.2–8.5 and Laravel 11–13.

**Use this package when** your application makes many repeated Eloquent queries and you want a drop-in caching layer that stays in sync with your data without any manual bookkeeping.

### 🔄 Before &amp; After

[](#-before--after)

❌ **Without this package** — manual cache keys, manual invalidation:

```
$posts = Cache::remember('posts:active:page:1', 3600, function () {
    return Post::where('active', true)->with('comments')->paginate();
});

// And in every observer or event listener…
Cache::forget('posts:active:page:1');
// Hope you remembered every key variant! 😅
```

✅ **With this package** — add the trait, query normally:

```
// Just query. Caching and invalidation happen automatically. ✨
$posts = Post::where('active', true)->with('comments')->paginate();
```

### ✅ What Gets Cached

[](#-what-gets-cached)

- Model queries (`get`, `first`, `find`, `all`, `paginate`, `pluck`, `value`, `exists`)
- Aggregations (`count`, `sum`, `avg`, `min`, `max`)
- Eager-loaded relationships (via `with()`)

### 🚫 What Does Not Get Cached

[](#-what-does-not-get-cached)

- Lazy-loaded relationships — only eager-loaded (`with()`) relationships are cached. Use `with()` to benefit from caching.
- Queries using `select()` clauses — custom column selections bypass the cache.
- Queries inside transactions — cache is not automatically flushed when a transaction commits; call `flushCache()` manually if needed.
- `inRandomOrder()` queries — caching is automatically disabled since results should differ each time.

### 💾 Cache Drivers

[](#-cache-drivers)

DriverSupportedRedis✅ (recommended)Memcached✅APC✅DynamoDB✅Array❌File❌Database❌### 📋 Requirements

[](#-requirements)

- PHP 8.2+
- Laravel 11, 12, or 13

📦 Installation
--------------

[](#-installation)

```
composer require genealabs/laravel-model-caching

```

✨ The service provider is auto-discovered. No additional setup is required.

🚀 Getting Started
-----------------

[](#-getting-started)

Add the `Cachable` trait to your models. The recommended approach is a base model that all other models extend:

```
