title: Laravel - Pass variable to layout
body: <p>I have a layout in which I use for each loop for all my so-called "Sections". However, every time I make a new blade file and extend that specific layout, it continuously says that the variable is not defined. And that is right because the variable is never passed to the layout file. It does only works if I pass the variable to the new blade file that extends the layout. How can I use that specific variable in my layout file?</p>
  
  <p><strong>Layout file</strong></p>
  
  <pre><code>
  &lt;!doctype html&gt;
  &lt;html lang="{{ str_replace('_', '-', app()-&gt;getLocale()) }}"&gt;
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  
      &lt;!-- CSRF Token --&gt;
      &lt;meta name="csrf-token" content="{{ csrf_token() }}"&gt;
  
      &lt;title&gt;{{ config('app.name', 'Laravel') }}&lt;/title&gt;
  
      &lt;!-- Scripts --&gt;
      &lt;script src="{{ asset('js/app.js') }}" defer&gt;&lt;/script&gt;
  
  
      &lt;!-- Fonts --&gt;
      &lt;link rel="dns-prefetch" href="//fonts.gstatic.com"&gt;
      &lt;link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet"&gt;
  
      &lt;!-- Styles --&gt;
      &lt;link href="{{ asset('css/app.css') }}" rel="stylesheet"&gt;
      &lt;link href="{{ asset('css/styles.css') }}" rel="stylesheet"&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="app"&gt;
      &lt;div class="sidebar p-4 shadow-lg"&gt;
          &lt;ul class="list-unstyled"&gt;
              &lt;li class="sidebar-item"&gt;&lt;a href="{{ route('sections') }}"&gt;Sections&lt;/a&gt;&lt;/li&gt;
              @foreach($sections as $section)
                  &lt;li class="sidebar-item"&gt;&lt;a href=""&gt;{{ $section-&gt;name }}&lt;/a&gt;&lt;/li&gt;
              @endforeach
              &lt;li class="sidebar-item"&gt;&lt;a href="/"&gt;Go back home&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;
      &lt;/div&gt;
      &lt;div class="custom-container"&gt;
          @yield('content')
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;script type="text/javascript" src="{{ asset('js/jquery-3.4.1.min.js') }}"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="{{ asset('js/jquery-ui.min.js') }}"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="{{ asset('js/main.js') }}"&gt;&lt;/script&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>
  
  <p><strong>Service Provider</strong></p>
  
  <pre><code>&lt;?php
  
  namespace App\Providers;
  
  use App\Section;
  use Illuminate\Support\Facades\View;
  use Illuminate\Support\ServiceProvider;
  
  class ViewServiceProvider extends ServiceProvider
  {
      /**
       * Register any application services.
       *
       * @return void
       */
      public function register()
      {
          //
      }
  
      /**
       * Bootstrap any application services.
       *
       * @return void
       */
      public function boot()
      {
  
          // Using Closure based composers...
          view()-&gt;composer('layout.backend', function ($view) {
              $view-&gt;with('sections', Section::all());
          });
      }
  }
  
  </code></pre>