Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Model Caching

Laravel Model Caching Laravel Package

mikebronner/laravel-model-caching

Automatically caches Eloquent model and relationship queries, then invalidates them when data changes—no manual cache keys. Add a trait to your models to cut database load on read-heavy pages with self-invalidating, Laravel-native caching.

View on GitHub
Deep Wiki
Context7

mikebronner/laravel-model-caching adds automatic, self-invalidating caching to Eloquent models and their relationships. Add the caching trait to a model and every query result is cached transparently—no manual cache keys, no forgotten invalidation. Creating, updating, or deleting a model automatically flushes the relevant cache entries, keeping reads fast and data consistent. On read-heavy pages, it typically cuts database queries significantly.

  • Zero-config caching via a single trait on your models
  • Automatic invalidation on create, update, and delete
  • Relationship-aware caching and flushing
  • Works with Laravel 11–13 and PHP 8.2–8.5
  • Battle-tested with 335+ integration tests
Frequently asked questions about Laravel Model Caching
How do I install and set up mikebronner/laravel-model-caching?
Install it with Composer using composer require mikebronner/laravel-model-caching. The service provider is auto-discovered, so no manual registration is needed in Laravel. Publish the package config file, then add the Cachable trait to a base model so every model extending it inherits the caching behavior. No schema changes or query-layer rewrites are required.
Which Laravel and PHP versions does laravel-model-caching support?
The README advertises support for Laravel 11, 12, and 13, with integration tests run across PHP 8.2 through 8.5. The 13.2.0 changelog mentions a Laravel 11 removal, which most likely refers to a compatibility shim or test-matrix entry rather than a dropped version. Because this shipped in a minor release, verify Laravel 11 is still in the CI test matrix before upgrading an 11.x application. Check the repository workflow file rather than relying on the changelog wording alone.
How does automatic cache invalidation work for Eloquent queries?
The package decorates Eloquent's query builder, caching read results per model and flushing the relevant entries when model events fire on create, update, or delete. That means you do not have to hand-write cache keys or remember Cache::forget calls for normal Eloquent writes. The catch is that invalidation depends entirely on model events being triggered, so every writer in your codebase must go through Eloquent for correctness. Silent stale reads are the main go/no-go risk to evaluate before adopting it.
Does it cache relationships and eager-loaded queries?
Yes. Reads on a cachable model and its eager-loaded relations are cached together, and version 13.2.0 specifically fixed relation model resolution, strengthening the eager-loaded relation invalidation path. Lazy-loaded relations, however, silently bypass caching. If your application relies heavily on lazy loading, profile those code paths separately before assuming you are getting cache coverage.
How do I disable model caching temporarily, for example in tests or while debugging?
The package exposes disableCache() and runDisabled() escape hatches, plus a global environment flag for switching caching off entirely. These are useful in test suites, seeders, and debugging sessions where you need fresh reads from the database. Use them in a scoped way so you do not accidentally leave caching off in staging or production.
What configuration options does the package provide?
The published config file exposes a small, sensible surface: enabled, store, cache-prefix, use-database-keying, and fallback-to-database. Database keying is enabled by default, which is the correct choice for multi-tenant or multi-database applications. You can point the package at any cache backend you already operate, so no new infrastructure is introduced. Most teams will only need to set the store and leave the rest at the defaults.
Is laravel-model-caching safe for multi-tenant or multi-database applications?
Yes, provided you leave use-database-keying enabled, which is the default. That setting includes the database in the cache key so tenants or connections do not collide on shared cache entries. Combined with the cache-prefix option, this lets you isolate cached data per tenant without custom key logic. Verify your queue workers and any non-default connections resolve the same database context as your web requests.
Which Eloquent queries silently bypass or break the cache?
Queries using select() clauses, lazily loaded relations, and inRandomOrder() silently bypass caching. More importantly, query-builder mass updates such as Post::where(...)->update(), raw DB::table() writes, and direct database access invalidate nothing. These coverage cliffs are invisible at runtime, so document them for your team and route writes through Eloquent models wherever possible.
Is the cache flushed automatically after a database transaction commits?
No. Cache entries are not flushed on commit, so you must call flushCache() manually when a multi-step transactional write completes. Any transaction that touches several models should get a review pass to confirm invalidation happens at the right moment. Skipping this step is one of the most common sources of stale reads with model caching.
Should I use this package instead of Cache::remember, and what maintenance risks should I watch?
It replaces the bespoke Cache::remember and Cache::forget bookkeeping that teams routinely get wrong, which is a real win on read-heavy pages where the README claims 100 to 900 percent query reduction. The trade-off is that correctness depends on model-event discipline and on cache key behavior, which is a public contract: version 13.2.0 changed key building for DateTimeImmutable bindings on a minor bump, so date-bounded queries cold-start after upgrading. Read the changelog before every upgrade, including minors, and confirm the CI matrix backs the Laravel version you run. The package is MIT licensed and actively maintained, but with a low recorded dependent count, treat it as a dependency you may have to own or fork.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
vento/laravel-airbrake
wfeller/laravel-batch
maco-studios/mage-os-installer
busarm/armie
siberfx/laravel-logger
kfteam1991/dcat-admin
dashed/dashed-laposta
dashed/dashed-ecommerce-montaportal
dashed/dashed-ecommerce-etsy
neohq/string-blade-compiler
log1x/acf-composer
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core
nodisrupt/laravel-agent
sierratecnologia/media-manager