Skip to content
Remember your Query results using only one method. Yes, only one.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Changed PHPDoc classes order. Dec 30, 2019

Patrick Perkins - Unslash (UL) #ETRPjvb0KM0

Latest Version on Packagist License Coverage Status

Rememberable Queries

Remember your Query results using only one method. Yes, only one.

User::latest()->where('name', 'Joe')->remember()->get();


  • PHP 7.2 or latest
  • Laravel 5.8|6.x
  • A working brain


You can install the package via composer:

composer require darkghosthunter/rememberable-query


Just use the remember() method to remember a Query result. That's it.

use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User;

$query = DB::table('users')->remember()->where('name', 'Joe')->first();

$eloquent = User::where('name', 'Joe')->remember()->first();

The next time you call the same query, the result will be retrieved from the cache instead of running the SQL statement in the database.

If the result is null or false, it won't be remembered, which mimics the Cache behaviour on these values.


By default, queries are remembered by 60 seconds, but you're free to use any length, Datetime, DateInterval or Carbon instance.

User::where('name', 'Joe')->remember(today()->addHour())->first();

Custom Cache Key

By default, the cache key is an MD5 hash of the SQL query and bindings, which avoids any collision with other queries. You can use any string, but is recommended to append query|{key} to avoid conflicts with other cache keys in your application.

User::where('name', 'Joe')->remember(30, 'query|find_joe')->first();

Custom Cache

In some scenarios, using the default cache of your application may be detrimental compared to the database performance. You can use any other Cache by telling the Service Container to pass it to the RememberableQuery class (preferably) in your AppServiceProvider.

public function boot()
        ->give(function () {
            return cache()->store('redis');
    // ...

Mind the gap

There are two things you should be warned about.

Operations are NOT commutative

Altering the Builder methods order may change the automatic cache key generation. Even if they are practically the same, the order of statements makes them different. For example:


// "query|fecc2c1bb6396e485d94eede60532937"

// "query|3ac5eba7cd0ef6151481bdfe46f6c22f"

If you plan to remember the same query on different parts of your application, it's recommended to set manually the same Cache Key to ensure hitting the cached results.

Only works for SELECT statements

The nature of remembering a Query is to cache the result automatically.

Caching the result for UPDATE, DELETE and INSERT operations will cache the result and subsequents operations won't be executed, returning unexpected results.

Don't use remember() on anything that is not a SELECT statement.


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

You can’t perform that action at this time.