Home > other >  How to get config in blade file with Laravel 8.x
How to get config in blade file with Laravel 8.x

Time:09-28

Here is my problem i've read already a lot of thing, I am actually following this tutorial

  1. Error#1

    <li >
            <a  href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            {{ config('languages')[App::getLocale()] }}
            </a>
            <div  aria-labelledby="navbarDropdownMenuLink">
                @foreach (config('languages') as $lang => $language)
                        @if ($lang != App::getLocale())
                            <a  href="{{ route('lang.switch', $lang) }}"> {{$language}}</a>
                        @endif
                @endforeach
            </div>
    
        </li>
    

    Or I get the error telling me that my array is empty (The third line is creating the error): Error2

        <li >
            <a  href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            {{ config('languages.*')[App::getLocale()] }}
            </a>
            <div  aria-labelledby="navbarDropdownMenuLink">
                @foreach (config('languages') as $lang => $language)
                        @if ($lang != App::getLocale())
                            <a  href="{{ route('lang.switch', $lang) }}"> {{$language}}</a>
                        @endif
                @endforeach
            </div>
    
        </li>
    

    Or I get the error telling me it needs a string not an array:

    Here is how my config is made: config/languages.php

        <?php
    
    return [
            'en' => [
                'display' => 'English',
                'flag-icon' => 'us',
            ],
    
            'fr' => [
                'display' => 'Français',
                'flag-icon' => 'fr',
            ],
    
            'es' => [
                'display' => 'Spanish',
                'flag-icon' => 'es',
            ],
    ];
    

    I also tried this and tried to access it with config(languages.languages):

        <?php
    
    return [
        'languages' => [
            'en' => [
                'display' => 'English',
                'flag-icon' => 'us',
            ],
    
            'fr' => [
                'display' => 'Français',
                'flag-icon' => 'fr',
            ],
    
            'es' => [
                'display' => 'Spanish',
                'flag-icon' => 'es',
            ],
        ],
    ];
    

    CodePudding user response:

    I simply copied and pasted everything from the tutorial for the 10th time and it worked, IDK why, tho I changed Config::get by config()

    CodePudding user response:

    For your problem, I have one suggestion for using proper helper methods.

    Use => Config::get('languages')

  • Related