title: Access user in AppServiceProvider?
body: <p>I need to be able to pass data to all views when the user is logged in.</p>
  
  <p>Here is what my <code>AppServiceProvider</code> currently looks like:</p>
  
  <pre><code>class AppServiceProvider extends ServiceProvider
  {
      public function boot()
      {
          if (Auth::check())
          {
              View::share('key', 'value');
          }
      }
  }
  </code></pre>
  
  <p>However, the data is never passed to the view because I can't use <code>Auth</code> (for some reason) in <code>AppServiceProvider</code> so the if statement condition is never met.</p>
  
  <p>Following a <a href="https://stackoverflow.com/a/37373424/7971549">previous solution</a> I found, I tried doing this:</p>
  
  <pre><code>class AppServiceProvider extends ServiceProvider
  {
      public function boot()
      {
          View::composer('*', function ($view)
          {
              if (Auth::check())
              {
                  View::share('key', 'value');
              }
          }
      }
  }
  </code></pre>
  
  <p>This worked, but the problem is that the closure is called whenever a view is rendered.</p>
  
  <p>For example, if my blade template includes 3 other blade templates (which it does), then the closure above will be called 4 times. <a href="https://laracasts.com/discuss/channels/laravel/using-auth-in-service-provider-to-get-user-object/replies/79555" rel="nofollow noreferrer">Explained further here</a>.</p>
  
  <p>So how can I access the user correctly in <code>AppServiceProvider</code>?</p>