Laravel - Reading data from cache file inside GlobalScope apply function












1














I am trying to read some data from cached filed previously stored after user has logged in.



If auth()->attempt($credentials) passes i store some users data to cache file.



$user_id = auth()->user()->id;
$user = User::with(['roles.perms','offices.clients','offices.locations.country'])->find($user_id);
cache()->put('user_'.$user_id.'_data',$user,60);


I want to use that cache file so I don't have to rerun some queries to get ID-s related for this user and its relations with other models.



So I am trying to read from that file inside GlobalScope apply function, lets say I want to display countries where the user have offices.



I tried this:



public function apply(Builder $builder, Model $model){
$user_id = auth()->user()->id;
$user = cache()->get('user_'.$user_id.'_data');
$countries = ;
foreach ($user->offices as $office){
$countries = $office->locations[0]->country_id;
}
$builder->whereIn($model->getTable().'.id',$countries);
}


If the cache is empty, it returns error "Trying to get property 'offices' of non-object".



If I turn off the global scope, log in, and then turn scope back, it works. So the cache file is created after login.



If I clear the cache and try to log in (with global scope) I get the error. So I am not sure, but I think somehow adding global scope executes before caching after login.



Any ideas how can I fix this or modify the procedure.



UPDATED



It looks like it took some time to create the file and global scope to read from it. I check if there is a file already, if not to create it. After this modification its working...



I used Model to create cache file not auth controller like I did before.



if($id=auth()->user()->id){
if(!Cache::has('user_'.$id.'_data')){
$user = User::with(['roles.perms','clients','offices.users','offices.employees','offices.clients.clientUsers','offices.locations.country','offices.projects.jobOrders'])->find($id);
Cache::forever('user_'.$id.'_data',$user);
}
if(!auth()->user()->hasRole(['dekra_master_admin'])) {
static::addGlobalScope(new EmployeeScope);
}
}









share|improve this question





























    1














    I am trying to read some data from cached filed previously stored after user has logged in.



    If auth()->attempt($credentials) passes i store some users data to cache file.



    $user_id = auth()->user()->id;
    $user = User::with(['roles.perms','offices.clients','offices.locations.country'])->find($user_id);
    cache()->put('user_'.$user_id.'_data',$user,60);


    I want to use that cache file so I don't have to rerun some queries to get ID-s related for this user and its relations with other models.



    So I am trying to read from that file inside GlobalScope apply function, lets say I want to display countries where the user have offices.



    I tried this:



    public function apply(Builder $builder, Model $model){
    $user_id = auth()->user()->id;
    $user = cache()->get('user_'.$user_id.'_data');
    $countries = ;
    foreach ($user->offices as $office){
    $countries = $office->locations[0]->country_id;
    }
    $builder->whereIn($model->getTable().'.id',$countries);
    }


    If the cache is empty, it returns error "Trying to get property 'offices' of non-object".



    If I turn off the global scope, log in, and then turn scope back, it works. So the cache file is created after login.



    If I clear the cache and try to log in (with global scope) I get the error. So I am not sure, but I think somehow adding global scope executes before caching after login.



    Any ideas how can I fix this or modify the procedure.



    UPDATED



    It looks like it took some time to create the file and global scope to read from it. I check if there is a file already, if not to create it. After this modification its working...



    I used Model to create cache file not auth controller like I did before.



    if($id=auth()->user()->id){
    if(!Cache::has('user_'.$id.'_data')){
    $user = User::with(['roles.perms','clients','offices.users','offices.employees','offices.clients.clientUsers','offices.locations.country','offices.projects.jobOrders'])->find($id);
    Cache::forever('user_'.$id.'_data',$user);
    }
    if(!auth()->user()->hasRole(['dekra_master_admin'])) {
    static::addGlobalScope(new EmployeeScope);
    }
    }









    share|improve this question



























      1












      1








      1


      0





      I am trying to read some data from cached filed previously stored after user has logged in.



      If auth()->attempt($credentials) passes i store some users data to cache file.



      $user_id = auth()->user()->id;
      $user = User::with(['roles.perms','offices.clients','offices.locations.country'])->find($user_id);
      cache()->put('user_'.$user_id.'_data',$user,60);


      I want to use that cache file so I don't have to rerun some queries to get ID-s related for this user and its relations with other models.



      So I am trying to read from that file inside GlobalScope apply function, lets say I want to display countries where the user have offices.



      I tried this:



      public function apply(Builder $builder, Model $model){
      $user_id = auth()->user()->id;
      $user = cache()->get('user_'.$user_id.'_data');
      $countries = ;
      foreach ($user->offices as $office){
      $countries = $office->locations[0]->country_id;
      }
      $builder->whereIn($model->getTable().'.id',$countries);
      }


      If the cache is empty, it returns error "Trying to get property 'offices' of non-object".



      If I turn off the global scope, log in, and then turn scope back, it works. So the cache file is created after login.



      If I clear the cache and try to log in (with global scope) I get the error. So I am not sure, but I think somehow adding global scope executes before caching after login.



      Any ideas how can I fix this or modify the procedure.



      UPDATED



      It looks like it took some time to create the file and global scope to read from it. I check if there is a file already, if not to create it. After this modification its working...



      I used Model to create cache file not auth controller like I did before.



      if($id=auth()->user()->id){
      if(!Cache::has('user_'.$id.'_data')){
      $user = User::with(['roles.perms','clients','offices.users','offices.employees','offices.clients.clientUsers','offices.locations.country','offices.projects.jobOrders'])->find($id);
      Cache::forever('user_'.$id.'_data',$user);
      }
      if(!auth()->user()->hasRole(['dekra_master_admin'])) {
      static::addGlobalScope(new EmployeeScope);
      }
      }









      share|improve this question















      I am trying to read some data from cached filed previously stored after user has logged in.



      If auth()->attempt($credentials) passes i store some users data to cache file.



      $user_id = auth()->user()->id;
      $user = User::with(['roles.perms','offices.clients','offices.locations.country'])->find($user_id);
      cache()->put('user_'.$user_id.'_data',$user,60);


      I want to use that cache file so I don't have to rerun some queries to get ID-s related for this user and its relations with other models.



      So I am trying to read from that file inside GlobalScope apply function, lets say I want to display countries where the user have offices.



      I tried this:



      public function apply(Builder $builder, Model $model){
      $user_id = auth()->user()->id;
      $user = cache()->get('user_'.$user_id.'_data');
      $countries = ;
      foreach ($user->offices as $office){
      $countries = $office->locations[0]->country_id;
      }
      $builder->whereIn($model->getTable().'.id',$countries);
      }


      If the cache is empty, it returns error "Trying to get property 'offices' of non-object".



      If I turn off the global scope, log in, and then turn scope back, it works. So the cache file is created after login.



      If I clear the cache and try to log in (with global scope) I get the error. So I am not sure, but I think somehow adding global scope executes before caching after login.



      Any ideas how can I fix this or modify the procedure.



      UPDATED



      It looks like it took some time to create the file and global scope to read from it. I check if there is a file already, if not to create it. After this modification its working...



      I used Model to create cache file not auth controller like I did before.



      if($id=auth()->user()->id){
      if(!Cache::has('user_'.$id.'_data')){
      $user = User::with(['roles.perms','clients','offices.users','offices.employees','offices.clients.clientUsers','offices.locations.country','offices.projects.jobOrders'])->find($id);
      Cache::forever('user_'.$id.'_data',$user);
      }
      if(!auth()->user()->hasRole(['dekra_master_admin'])) {
      static::addGlobalScope(new EmployeeScope);
      }
      }






      php caching eloquent laravel-5.7 global-scope






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 3 '18 at 12:43







      user3605921

















      asked Nov 23 '18 at 11:14









      user3605921user3605921

      64




      64
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445648%2flaravel-reading-data-from-cache-file-inside-globalscope-apply-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445648%2flaravel-reading-data-from-cache-file-inside-globalscope-apply-function%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          What visual should I use to simply compare current year value vs last year in Power BI desktop

          Alexandru Averescu

          Trompette piccolo