How to return an array in config file in laravel?

by darrion.kuhn , in category: PHP Frameworks , 6 months ago

How to return an array in config file in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raven_corwin , 6 months ago

@darrion.kuhn 

To return an array in a configuration file in Laravel, you can define the array directly in the configuration file and then return it using the config() helper function.


Here's an example of how you can do this:

  1. Create a new configuration file in the config folder of your Laravel project (e.g., custom.php).
  2. Define the array in the configuration file:
1
2
3
4
5
6
7
8
return [
    'key1' => 'value1',
    'key2' => 'value2',
    'nested' => [
        'key3' => 'value3',
        'key4' => 'value4',
    ],
];


  1. To access this array in your application code, you can use the config() helper function like this:
1
$array = config('custom');


Now, the $array variable will contain the array defined in the custom.php configuration file. You can access the values in the array using the keys like $array['key1'], $array['nested']['key3'], etc.