Cache of all records from request in laravel 5

I try to cache all request records for 60 minutes with the following method (method 1)

Route::get('categoryList', function() { return app\CategoryDetails::remember(60)->get(); }); 

I followed this tutorial link (Tip 5: Cache Database Requests)

But I get this error:

Call the undefined method Illuminate\Database\Query\Builder::remember()

I don’t know what I am missing here.

By the way, I know that I can cache entire records with the following method (method 2):

 Route::get('categoryList', function() { $category = Cache::remember('category', 10, function() { return \App\CategoryDetails::all(); }); return $category; }); 

and it works great.

I'm just wondering why the first method doesn't work for me.

+5
source share
2 answers

Laravel 5 removed this functionality. Now you should keep the cache for yourself :

 Route::get('categoryList', function () { return Cache::remember('category-details', 60, function () { return App\CategoryDetails::all(); }); }); 

From the docs :

Eloquent no longer provides a remember method for caching queries. You are now responsible for caching queries manually using the Cache::remember function.

+9
source

Consider using the larvel eloquent request caching library called rememberable

This is a very good job.

0
source

Source: https://habr.com/ru/post/1232033/


All Articles