This package is abandoned and no longer maintained. The author suggests using the varbox/varbox package instead.

Cache all {select} queries or only the duplicate ones for a specific Eloquent model

3.0.0 2020-06-09 08:13 UTC

This package is auto-updated.

Last update: 2020-10-10 14:15:02 UTC


Unfortunately this package is now discontinued.
Please check out Varbox (Laravel Admin Panel) for this functionality and much more.

Thank you!

Cache all "select" queries or only the duplicate ones for a specific Eloquent model

Build Status StyleCI Scrutinizer Code Quality


This package allows you to cache all queries of type select, or only just the duplicated ones for an Eloquent model.

Please note that because cache tagging is used, "file" or "database" cache drivers are incompatible with this package.

Compatible cache stores: array, redis, apc, memcached
Tested cache stores: array, redis


Install the package via Composer:

composer require neurony/laravel-query-cache

Publish the config file with:

php artisan vendor:publish --provider="Neurony\QueryCache\ServiceProvider" --tag="config"

Please read the config/query-cache.php config file comments as it contains extra information


Step 1

Your Eloquent models should use the Neurony\QueryCache\Traits\IsCacheable trait.


namespace App;

use Illuminate\Database\Eloquent\Model;
use Neurony\QueryCache\Traits\IsCacheable;

class YourModel extends Model
    use IsCacheable;
Step 2

In your .env file add the necessary environment variables:

# The driver used for storing the cache keys that this package generates.
# This driver can differ from your main Laravel's CACHE_DRIVER.

# Wether to cache absolutely all queries for the current request.

# Wether to cache only the duplicated queries for the current request.

Depending on how you set your environment variables, the next time you make select queries on that Eloquent model, after the very first run, the queries will be cached.


Using the QueryCacheService class

Please note that the Neurony\QueryCache\Services\QueryCacheService class is the actual implementation of the Neurony\QueryCache\Contracts\QueryCacheServiceContract interface.

The Neurony\QueryCache\Services\QueryCacheService class is bound to the Laravel's IoC as a singleton and aliased as cache.query.

With that being said, the recommended way of directly using the Neurony\QueryCache\Services\QueryCacheService is:


// or

Enable / Disable query caching

You can enable or disable all query caching for your current request, by calling the enableQueryCache or disableQueryCache methods present on the Neurony\QueryCache\Services\QueryCacheService class.

// from her on, no queries will be cached

make your queries
the queries up until now won't be cached

// from her on, apply query caching

make your queries
this queries will be cached



If you discover any security related issues, please email instead of using the issue tracker.


The MIT License (MIT). Please see LICENSE for more information.


Please see CHANGELOG for more information on what has changed recently.


Please see CONTRIBUTING for details.