简体   繁体   中英

Laravel Get Config Variable

In Laravel 5.0 (I know it is old, but the project is not mine) I set in config/app.php

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?

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.

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'); 
$languages = config('app.languages');

print_r($languages);

Get More Details with Placement Question Article

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM