Daniel81
Daniel81

Has anyone managed to get this working?

Basically, if you use the built-in caching from Laravel/October found here, you can cache your database call and return it as an array, but returning a model/collection will result in a "serialisation error", therefore not allowing you to use all the good stuff linked to your model e.g images, relations etc.

For example, say you had a products plugin and you had 100s of products in your database and you wanted to cache the results for 60 minutes instead of hitting the database every time someone visited the page, you might do something like this :

$products = Cache::remember("all_products", 60, function() {
        return DB::table("products")->get();
});

which would work fine if you wanted your results as an Array, but if you wanted to use Eloquent to return your results like:

$products = Cache::remember("all_products", 60, function() {
        return ProductModel::all();
});

this would return an a "serialisation error" due to the closure returning a model/collection. You would have to return ProductModel::all()->toArray(); which would return the same as example 1, but this defeats the object as you really want to be able to use all the items linked to the model.

Hope that all makes sense.

Thanks in advance for anyone who can help :)

Last updated

Daniel81
Daniel81

Have found the package watson/rememberable which re-enables the ->remember() option for eloquent caching - package here

Last updated

floriskoch
floriskoch

What was your experience using that package? Does it work properly? Thanks for the tip though!

gavinfostered11132
gavinfostered11132

The watson/rememberable package may not be necessary now:

https://github.com/octobercms/october/issues/2639

"remember" was brought back by October when Laravel killed it and returns October query builders on both db facade and models. Also worth noting some updates to latest October builds using Laravel 5 LTS regarding collections and arrays using get() documented in this support entry:

http://octobercms.com/support/article/rn-9

1-4 of 4