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