0

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());
        });
    }
}

2
  • what variable in your layout? sections? Commented Dec 15, 2019 at 13:25
  • Yes that is the only variable used in de layout. Commented Dec 15, 2019 at 13:28

1 Answer 1

0

You can use a View Composer to pass the data needed specifically to the layout when it is rendered. You can define this in a Service Provider's boot method:

use Illuminate\Support\Facades\View;

public function boot()
{
    View::composer('layout.yourlayout', function ($view) {
        $view->with('sections', ...);
    });
}

Laravel 6.x Docs - Views - View Composers composer

Sign up to request clarification or add additional context in comments.

5 Comments

Could you elaborate me on this? Give a more detailed explanation?
more detailed explanation of what?
How the above code is supposed to be implemented. I implemented the code and added the service provider to the config.app.php but it does not seem to work (See updated answer)
make sure you configuration isn't cached php artisan cache:clear
It does not seem to change anything

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.