问题
I am learning laravel eloquent in Laravel 5.6.
I have already used database query builder & cache.
Retrieve & Store for Database Query Builder
$value = Cache::remember('users', $minutes, function () {
return DB::table('users')->get();
});
But i don't know how to cache for eloquent.
Retrieve & Store for Eloquent
$value = Cache::remember('users', $minutes, function () {
return App\User::all();
});
Is it correct for eloquent or any other method existing?
回答1:
There is a built-in method for this:
App\User::where(['id' => 1])->cache()
I hope this helps, although a year later.
来源:https://stackoverflow.com/questions/51237161/laravel-eloquent-model-cache