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?
Layout file
<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', 'Laravel') }}</title>
<!-- Scripts -->
<script src="{{ asset('js/app.js') }}" defer></script>
<!-- Fonts -->
<link rel="dns-prefetch" href="//fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<link href="{{ asset('css/styles.css') }}" rel="stylesheet">
</head>
<body>
<div id="app">
<div class="sidebar p-4 shadow-lg">
<ul class="list-unstyled">
<li class="sidebar-item"><a href="{{ route('sections') }}">Sections</a></li>
@foreach($sections as $section)
<li class="sidebar-item"><a href="">{{ $section->name }}</a></li>
@endforeach
<li class="sidebar-item"><a href="/">Go back home</a></li>
</ul>
</div>
<div class="custom-container">
@yield('content')
</div>
</div>
<script type="text/javascript" src="{{ asset('js/jquery-3.4.1.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/jquery-ui.min.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/main.js') }}"></script>
</body>
</html>
Service Provider
<?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()->composer('layout.backend', function ($view) {
$view->with('sections', Section::all());
});
}
}
sections?