renoki-co / laravel-eloquent-query-cache

Adding cache on your Laravel Eloquent queries' results is now a breeze.
Apache License 2.0
1.04k stars 108 forks source link

DontCache doesn't apply to a relation #194

Closed milosb793 closed 1 year ago

milosb793 commented 1 year ago

When using Model::dontCache() method, seems like it doesn't apply to relations. Model don't cache, but relations does. Here's an example:

User::noCache()
  ->with(['team'])
  ->where('email', 'user@example.com')
  ->first();

In this case, a user will not be cached, but a team will.

Is there any workaround for this?

rennokki commented 1 year ago

@milosb793 The relationships is not inheriting any cache settings from the main query.

User::noCache()
+  ->with(['team' => fn ($q) => $q->noCache()])
-  ->with(['team'])
  ->where('email', 'user@example.com')
  ->first();
milosb793 commented 1 month ago

Just a heads-up if anyone else is having such issues, it's dontCache() instead of noCache():

User::noCache()
  ->with(['team' => fn ($q) => $q->dontCache()])
  ->with(['team'])
  ->where('email', 'user@example.com')
  ->first();