title: How can I get the ID of logged in user and use it in my AppServiceProvider file in Laravel 7?
body: <p>I'm working on my application using Laravel 7. I have used View Composer in my AppServiceProvider to get the count for a number of items in my database table. I have used where clause to get count for a specific logged in user. The problem is that I'm not sure how I can get the ID of currently logged in User and use in my AppServiceProvider. I have tried doing it but I'm getting <code>Undefined variable: userId</code> error. Please help. Thanks.</p>
  <p>AppServiceProvider:</p>
  <pre><code>&lt;?php
  
  namespace App\Providers;
  
  use App\Models\Project;
  
  use Illuminate\Support\ServiceProvider;
  use Illuminate\Support\Facades\View;
  use Illuminate\Support\Facades\Auth;
  
  class AppServiceProvider extends ServiceProvider
  {
      /**
       * Register any application services.
       *
       * @return void
       */
      public function register()
      {
          //
      }
  
      /**
       * Bootstrap any application services.
       *
       * @return void
       */
      public function boot()
      {
          $userId = Auth::id();
          
          View::composer('client_panel.layouts.menu', function ($view) {
              $view-&gt;with('newprojects', Project::where([
                  ['status','=','1'],
                  ['created_by','=', $userId]
               ])-&gt;count());
          });
      }
  }
  </code></pre>