38

In Laravel 5.0 I have set in config/app.php this:

return [
//...
'languages' => ['en','it'],
//...
]

Then, I have a blade wrapper in resources/views/frontend/includes/menus/guest.blade.php

@foreach (Config::get('languages') as $lang => $language)

But, Laravel says that foreach has no valid argument, which means that Config::get('languages') returns null. I can't set custom variables in app.php?

4 Answers 4

44

You need to change it to:

@foreach (Config::get('app.languages') as $lang => $language).

Treat the first segment of your lookup as the files under /config, in this case app.php corresponds to Config::get('app.*')

If it wasn't obvious, you can use the helper function config() rather than Config::get() as well.

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

5 Comments

Have you cached your config? php artisan config:clear
Thank you, I did it and now work. However, config in cache??I would prefer Cake to Laravel...
Now I have "No hint path defined for [cookieConsent]. "
I promise you, in the long run, you'll prefer Laravel. 5.0 is quite old now. If you can, use 5.5 It is the latest LTS version
Now the last error:FatalThrowableError in ProviderRepository.php line 119: Call to undefined method Illuminate\Html\FormFacade::isDeferred()
43

Laravel has a helper function for config which allows you to avoid instantiating a Config instance each time you access a value.

Simply use:

config('app.languages'); 

1 Comment

What is the most common way to test code that uses an environment variable in this way. Normally i would guess dependency injection, however i don't see how that would work here.
5
$languages = config('app.languages');

print_r($languages);

1 Comment

This only duplicates Luke's much earlier answer.
1

To also add to what n3storm said, if you creat a new config file and you want to call it on your blade.php, it should be

<h1>{{config('file_name.variable_name')}}</h1>

with above code, everything should work.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.