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 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
13.2.0

💥 Breaking Changes

1. PHP 8.2 is no longer supported

13.2.0 requires PHP 8.3 or later (#645). On PHP 8.2, Composer stays on 13.1.11.

2. $isCachable is typed bool

The trait now declares protected bool $isCachable (#647). A model or builder that declares $isCachable again without the bool type fails with a fatal error when the class loads. Fix: declare it as protected bool $isCachable.

3. The trait no longer has newBaseQueryBuilder()

13.1.8 added newBaseQueryBuilder() to the Cachable trait. It collided with other traits that define the same method, such as CTE support, so it is gone (#640, #651). A trait rule that names Cachable::newBaseQueryBuilder now fails when the class loads:

  • Cachable::newBaseQueryBuilder as ...
  • Cachable::newBaseQueryBuilder insteadof ...

Fix: delete that rule. Other::newBaseQueryBuilder insteadof Cachable still works.

4. modelCache:clear no longer empties the whole cache store

On Redis, the command used to wipe the app's entire cache store, or the whole Redis database when the prefix was empty (#645). It now deletes only the package's own entries. Fix: if a script relied on it to clear everything, add php artisan cache:clear.

5. Some cache keys and tags change

These queries miss the cache once after upgrading, then rebuild:

  • Closure where(), orWhere() and whereNot() queries on models with global scopes (#647).
  • morphTo eager loads onto models with global scopes (#647).
  • withoutGlobalScopesExcept() calls that keep every scope (#647).
  • Eager-load constraints that do more than add where clauses (#647).
  • belongsToMany and morphToMany queries onto scoped models, or that use withPivot(), withTimestamps(), a custom as(), using(), with(), or a related model with $with (#647).
  • fromSub(), union and beforeQuery() bindings that are Stringable objects with non-public state (#647).
  • Eager loads of a belongsToMany or morphToMany relation that do not join the pivot table, such as with('stores') (#641, #653).
  • Through-relation queries that use with() (#647), or that forward a macro or custom builder method (#656).
  • Every macro or custom builder method call with arguments (#656). The arguments now key as a JSON list, because null, false and "" used to share a key, as did 1 and "1".
  • Cool-down keys on models that declare $cachePrefix (#651). A cool-down window that is open at upgrade ends early once.

6. Some queries now run uncached

These used to be cached under a key that another query could share, so they could return the wrong rows. They now always read from the database:

  • A where() or having() value that cannot be keyed reliably, such as an object serialize() rejects or a value that holds a resource (#647).
  • An eager-load constraint with an afterQuery() callback (#647).
  • A relation query that eager-loads a model without Cachable (#647).
  • A macro or custom builder method that gets a closure, an object without __toString(), or an array json_encode cannot encode (#656).

⬆️ Upgrade steps

  1. Run php artisan modelCache:clear after you deploy. Some changed queries keep their old key text, so until the next write to that table they could read an entry the old version wrote.
  2. Check your models for an untyped $isCachable and for trait rules that name newBaseQueryBuilder (items 2 and 3).

Changes

Added

Fixed

New Contributors

Full Changelog: https://github.com/mike-bronner/laravel-model-caching/compare/13.1.11...13.2.0

13.1.11

Fixed

New Contributors

Full Changelog: https://github.com/mike-bronner/laravel-model-caching/compare/13.1.10...13.2.0

13.1.7

Fixed

Changed

New Contributors

Full Changelog: https://github.com/mike-bronner/laravel-model-caching/compare/13.1.6...13.1.7

13.1.6

Fixed

Full Changelog: https://github.com/mike-bronner/laravel-model-caching/compare/13.1.5...13.1.6

13.1.3

Fixed

Full Changelog: https://github.com/mike-bronner/laravel-model-caching/compare/13.1.2...13.1.3

13.1.2

Fixed

New Contributors

Full Changelog: https://github.com/mike-bronner/laravel-model-caching/compare/13.1.1...13.1.2

13.1.1

Fixed

Full Changelog: https://github.com/mike-bronner/laravel-model-caching/compare/13.1.0...13.1.1

13.0.6

Fixed

  • composer.json typos.
13.0.5

Changed

  • repo owner from mikebronner private account to mike-bronner org account.
13.0.4

Fixed

Full Changelog: https://github.com/mikebronner/laravel-model-caching/compare/13.0.3...13.0.4

13.0.2

Removed

  • 🔥 conflicting composer dev dependencies that prevented installation in Laravel 13.

Full Changelog: https://github.com/mikebronner/laravel-model-caching/compare/13.0.1...13.0.2

13.0.1

Fixed

  • 🚨 LaraStan warnings related to EloquentBuilder.

Full Changelog: https://github.com/mikebronner/laravel-model-caching/compare/13.0.0...13.0.1

13.0.0

✨ Highlights

This release adds Laravel 13 support, introduces several powerful new features — including a ModelCache Facade, graceful fallback when the cache backend is down, and smarter cache invalidation — and fixes a batch of edge-case bugs.

⬆️ Upgrade Steps

  1. Update your composer.json:
    composer require mikebronner/laravel-model-caching
    
  2. (Optional) If you want to use the new graceful fallback feature, add to your .env:
    MODEL_CACHE_FALLBACK_TO_DB=true
    
  3. (Optional) Publish the updated config to get the new fallback-to-database option:
    php artisan modelCache:publish --config
    

⚠️ Breaking Changes

Laravel 10 dropped — Minimum Laravel version is now 11. If you are still on Laravel 10, stay on v12.x.
PHP 8.1 dropped — Minimum PHP version is now 8.2. If you are still on PHP 8.1, stay on v12.x.

Existing applications on Laravel 11 or 12 with PHP 8.2+ should be able to upgrade without any code changes.

🚀 New Features

  • Laravel 13 support — Bumped all illuminate/* dependencies to accept ^13.0. (#557)
  • ModelCache Facade — New ModelCache::invalidate() Facade for programmatic cache flushing without Artisan commands. Accepts a single model class or an array. (#571)
  • Graceful fallback to database — When enabled via MODEL_CACHE_FALLBACK_TO_DB=true, the package logs a warning and falls back to direct database queries if the cache backend (e.g. Redis) is unavailable, instead of throwing an exception. (#574)
  • Cache tags for joined tables — Queries using join(), leftJoin(), etc. now generate cache tags for the joined tables, so writes to joined models correctly invalidate the cache. (#577)
  • Smarter delete/forceDelete invalidation — Cache is now only flushed when delete() or forceDelete() actually removes records (affected rows > 0), avoiding unnecessary invalidation on no-op deletes. (#578)
  • Custom Eloquent builder support — Models that define a custom newEloquentBuilder() method now work correctly with the Cachable trait. (#528)
  • MorphTo and morphedByMany invalidation — Deleting a morphTo-related model or using attach/detach on morphedByMany relationships now correctly flushes the cache. (#575)
  • Bypass cache for pessimistic locking — Queries using lockForUpdate() or sharedLock() now automatically bypass the cache, since locked queries should always hit the database. (#573)

🐛 Bug Fixes

  • belongsToMany()->sync() memory exhaustion — Fixed a memory leak when syncing large pivot datasets. (#569)
  • Cursor pagination crash — Fixed RowValues where type breaking compatibility with Laravel's cursor pagination. (#562)
  • Multi-domain paginator base URL — Paginated results now use the correct base URL per domain in multi-domain applications. (#561)
  • UUID validation crash in cache keys — Fixed a crash when building cache keys containing non-UUID binary strings. (#559)
  • Dynamic $fillable interference — The Cachable trait no longer interferes with models that modify $fillable dynamically (e.g. in __construct()). (#564)
  • Pivot model observer events — Custom pivot model observers now fire correctly on cached models. (#567)
  • Pivot cache invalidation with custom models — BelongsToMany relationships using custom pivot models (->using()) now correctly invalidate the cache on attach/detach/sync. (#566)
  • MorphTo eager-load cache tag resolution — Fixed cache tag generation for morphTo relationships in eager-load chains. (#579)
  • Model retrieved event on cache hit — The Eloquent retrieved event now fires correctly when results are served from cache, matching the behavior of uncached queries. (#563)
  • Missing EloquentBuilder import — Added a missing use import in CachedBuilder.php. (#558)

🧹 Housekeeping

  • Removed unnecessary Nova test fixtures and references.
  • Added SECURITY.md with vulnerability reporting instructions.
  • Added Dependabot configuration for automated dependency updates.
  • Comprehensive README overhaul with badges, table of contents, before/after examples, driver comparison table, and real-world usage examples.

🧪 Testing

  • 335+ integration tests across PHP 8.2–8.5 and Laravel 11–13.
  • New test coverage for: reorder() caching, delete/forceDelete flush behavior, custom builders, dynamic fillable, lock queries, cursor pagination, MorphTo eager loads, multi-domain pagination, and pivot model observers.

Full Changelog: https://github.com/mikebronner/laravel-model-caching/compare/12.0.4...13.0.0

12.1.0-RC1

Added

  • ✨ Support custom Eloquent builders on cachable models by @mikebronner #528

Full Changelog: https://github.com/mikebronner/laravel-model-caching/compare/12.0.3...12.1.0-RC1

12.0.3

Fixed

Removed

Full Changelog: https://github.com/mikebronner/laravel-model-caching/compare/12.0.2...12.0.3

12.0.2

Fixed

  • Nova tests to pass after upgrading to Nova 5.
12.0.1

Fixed

  • package dependency.
12.0.0

Added

Changed

  • namespace from genealabs/laravel-model-caching to mikebronner/laravel-model-caching, as this is a good time to make a breaking change, so that it properly reflects its new home.

Fixed

  • nullable method parameters. Thanks @d8vjork!
  • cache key issue. Thanks @JC5!
  • query attribute in strict mode. Thanks @daika7ana!
11.0.1

Fixed

  • dependency versions.
11.0.0

Added

0.13.9

Fixed

0.13.8

Fixed

0.13.7

Fixed

  • where clauses that were expressions. Thanks @padre!
0.13.6

Fixed

  • missing JsonContainsKey query type. Thanks @jamesRUS52!
0.13.5

Fixed

0.13.4

Fixed

  • attach() and detach() issues after upgrading to Laravel 10. Thanks @jason-klein
0.13.3

Fixed

  • 'second' in where clause if it is an Expression object.
0.13.2

Changed

  • pagination to be compatible with Laravel 10.3 by adding $total parameter. Thanks @joshuadwire!
0.13.1

Fixed

  • parsing of first if it is an Expression object. Thanks @vheins!
0.13.0

Added

  • Laravel 10 compatibility. Thanks @faytekin !
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections