Weave Code
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 Eloquent Query Cache

Laravel Eloquent Query Cache Laravel Package

vigstudio/laravel-eloquent-query-cache

Add query-level caching back to Eloquent with a simple remember-like API. Cache results from database queries, reduce repeated hits, and integrate with Laravel’s cache stores for faster reads and configurable invalidation.

View on GitHub
Deep Wiki
Context7

Adding cache on your Laravel Eloquent queries' results is now a breeze.

Frequently asked questions about Laravel Eloquent Query Cache
How do I install and enable query caching for Eloquent in Laravel?
Install via Composer with `composer require vigstudio/laravel-eloquent-query-cache`, then enable it globally in `AppServiceProvider` using `QueryCache::enable()`. For selective use, apply the `QueryCacheable` trait to specific models or use middleware to toggle caching per request.
Does this package support Laravel 10 or only older versions?
The package is designed for Laravel 8+ and should work with Laravel 10, but test thoroughly as Eloquent APIs evolve. Check the GitHub repo for compatibility notes or open an issue if you encounter version-specific issues.
How does cache invalidation work if data changes?
Invalidation relies on Laravel’s cache system (e.g., `Cache::forget()`). For automatic invalidation, listen to Eloquent events like `saved`, `deleted`, or `updated` and manually clear the cache keys. Example: `User::saved(fn() => Cache::forget('eloquent:User:all'))`.
Can I cache complex queries with joins or dynamic scopes?
The package works best with simple queries (e.g., `Model::all()`, `Model::where('active', true)`). Complex queries (e.g., raw joins, dynamic scopes) may require custom cache keys or exclusion logic. Test edge cases to avoid stale or inconsistent data.
What cache drivers does this package support?
It integrates with Laravel’s built-in cache drivers (Redis, Memcached, file, database, etc.). Configure your preferred driver in `config/cache.php`—the package will use the default or specified store unless overridden.
How do I bypass caching for specific queries or tests?
Use `QueryCache::disable()` to globally disable caching or apply the `->withoutCache()` method to individual queries. For testing, mock the cache facade or use `Cache::shouldReceive('get')->andReturn([])` in PHPUnit.
Will caching slow down my application if the cache misses frequently?
Cache misses add overhead, but the package is optimized for read-heavy workloads. Monitor cache hit/miss ratios in production (e.g., via Laravel Debugbar or custom logging) and adjust TTL or cache keys to improve efficiency.
Are there alternatives to this package for Eloquent caching?
Alternatives include Laravel’s built-in `Cache::remember()` (manual caching), `spatie/laravel-query-builder` (for query caching), or database-level caching (e.g., Redis with `SELECT` caching). This package offers a more seamless, model-level integration.
How do I structure cache keys to avoid collisions?
The package uses a default key format like `eloquent:{model}:{method}:{query_hash}`. Customize this in the config or override the `getCacheKey()` method in your models. Avoid dynamic keys for queries with identical parameters but different results.
Is this package actively maintained? What’s the upgrade path for Laravel updates?
The package is maintained by Renoki Co., with the last update in 2023. Monitor the GitHub repo for Laravel compatibility notes. If Eloquent APIs change, the package may require updates—check the changelog or contribute fixes if needed.
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.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor