laravel-model-caching: Global Scopes Not Being Busted Based On Called Context

Describe the bug When calling Model::all() - the global scope is not being applied. I am getting an instance of the first cached result, not the updated one when the Auth::user() changed in the global scope.

In my model I attach a global scope on boot which is a class implementing scope:

static::addGlobalScope(new SameCompanyScope);

However when I run the below Eloquent query, it always returns the cached results instead of updating the results when the scope changes due to a different user being logged in.

Eloquent Query Please provide the complete eloquent query that caused the bug, for example:

Model::all();

Stack Trace There is no stack trace, because nothing “breaks” it just returns the incorrect information.

Is this expected behaviour?

The workarounds I have identified include using the get() method on the model instead of all, but this does not feel correct as the global scope should be applied to all.

The other is modifying ModelCaching.php trait like:

if (!$instance->isCachable() || $instance->getGlobalScopes()) {
   return parent::all($columns);
}

To disable the caching when a global scope are being applied, but this still feels wrong. Any thoughts or tips would be appreciated.

Environment

  • PHP: 7.3.5
  • OS: Windows 10 - Wamp
  • Laravel: 6.15.1
  • Model Caching: 0.7.4

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

I jury rigged the changes as our codebase is still on Laravel 6, so could not install the latest version through composer. It seemed to work ok, but we are due to upgrade to laravel 7 in the next few weeks, so any problems I will update you then 😃 thanks for acting on the PR so quickly. Much appreciated