简体   繁体   中英

Laravel Query Builder: Caching a list result

It's possible to cache query result like so:

$users = DB::table('users')->remember(10)->get();

But how do I cache a list result. This doesn't work:

$roles = DB::table('roles')->lists('title'); // Works, but not cached.
$roles = DB::table('roles')->remember(10)->lists('title'); // Not working.

Error thrown:

exception 'BadMethodCallException' with message 'Call to undefined method Illuminate\Database\Query\Builder::remember()'

The Illuminate\\Database\\Query\\Builder::remember() was removed in laravel 5+, you can use this instead:

$roles = Cache::remember('roles', 10, function() {
    return DB::table('roles')->lists('title');
});

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM