Search code examples
laravelconfiglaravel-blade

Laravel Blade loop through config array variable


In my config/locale.php I have an array displayLanguage which contains 'key => value' pairs. How can I loop through this array in blade? I have tried the following:

@foreach ( {{ Config::get('app.locale.displayLanguage') }}  as $itemKey => $itemVal)
    {{ $itemKey }}
@endforeach

I am getting syntax error, unexpected '<'. Tried also some other variation to loop this var without passing it through the controller.


Solution

  • If your file is in config/locale.php then you call config('locale.displayLanguage');

    @foreach(config('locale.displayLanguage') as $key => $value)
        {{ $key }}
    @endforeach
    

    I am using the global helper config() in a blade file.

    It also appears you have extra curly braces in your foreach loop