Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Get Config Variable

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?

like image 854
gdm Avatar asked Mar 08 '18 22:03

gdm


People also ask

How get data from config file in Laravel?

We use the config() method to get values from files in the config directory. We use the dot notation to get the value we want. The first parameter we use is the file's name without the . php extension.

Where is config php in Laravel?

All of the configuration files for the Laravel framework are stored in the app/config directory. Each option in every file is documented, so feel free to look through the files and get familiar with the options available to you.

What is config function in Laravel?

What is the config method? The config method allows your application to get or set values in all files that are in the config directory.


1 Answers

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.

like image 144
Jonathan Avatar answered Sep 23 '22 21:09

Jonathan