Thursday, February 10, 2022

[FIXED] Laravel Get Config Variable

Issue

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?


Solution

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.



Answered By - Jonathan

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.