35

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?

francisco
  • 1,387
  • 2
  • 12
  • 23
gdm
  • 7,647
  • 3
  • 41
  • 71

3 Answers3

42

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.

Jonathan
  • 10,936
  • 8
  • 64
  • 79
  • 1
    Have you cached your config? `php artisan config:clear` – Jonathan Mar 08 '18 at 22:58
  • Thank you, I did it and now work. However, config in cache??I would prefer Cake to Laravel... – gdm Mar 08 '18 at 22:59
  • Now I have "No hint path defined for [cookieConsent]. " – gdm Mar 08 '18 at 23:00
  • 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 – Jonathan Mar 08 '18 at 23:02
  • Now the last error:FatalThrowableError in ProviderRepository.php line 119: Call to undefined method Illuminate\Html\FormFacade::isDeferred() – gdm Mar 08 '18 at 23:18
35

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'); 
vimuth
  • 5,064
  • 33
  • 79
  • 116
Luke
  • 943
  • 6
  • 12
  • 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. – yourivdloo Mar 06 '23 at 11:24
4
$languages = config('app.languages');

print_r($languages);

Get More Details with Placement Question Article

P S Solanki
  • 1,033
  • 2
  • 11
  • 26
Gaurav Porwal
  • 503
  • 3
  • 6