the Laravel caching mechanism works fine when doing:
$users = User::remember(10)->get();
but when doing:
$users = User::with('posts','addresses')->remember(10)->get();
it doesn't cache the entire set of queries, specifically the join query (eager loading).
Is there a way to cache all the queries that are performed in the above example? Thanks!
You can do it inline:
User::with(['posts' => function ($q) {
$q->remember(10);
}, 'addresses' => function ($q) {
$q->remember(10);
}])->remember(10)->get();
or in the relation definition:
public function posts()
{
return $this->hasMany('Post')->remember(10);
}
You cannot cache eagler loading queries in that way. You have 2 solutions to choose - cache::remember engine:
$users = Cache::remember('custom_cache_key', 10, function() {
return User::with('posts', 'addresses')->get();
});
or build single query using query builder:
...->select(...)->join(...)->where(...)->remember(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With