简体   繁体   中英

how to access variable from config file in laravel

I have one file inside config folder lets say: file1

File Location:
    config/
    ---file1.php

I have following code in my file: file1.php

return [
 'MASTER_KEY' => ['SUB_KEY1' => 'SOME_VALUE', 'SUB_KEY2' => 'SOME_VALUE', 'SUB_KEY3' => 'SOME_VALUE'],
];

How to access the value from MASTER_KEY of particular SUB_KEY ?

Assuming for SUB_KEY2 , Try -

config('file1.MASTER_KEY.SUB_KEY2')

Guide

In order to access that value you have 2 choices which are the same at the end:

first one: which use Config refers to the config facade.

use Config;

$myValue = Config::get('file1.MASTER_KEY.SUB_KEY1');
enter code here

second one: using config() helper function which uses Config facade and its only an alternative and easy way to use Config facade.

$myValue = config('file1.MASTER_KEY.SUB_KEY1');

Make a use Config; in your controller. In config/file1.php

<?php return [
    'ADMIN_NAME' => 'administrator',
    'EMP_IMG_PATH' => '../uploads/profile/original',
    'EMP_DOC_PATH' => '../uploads/profile/documents', ];

In controller

use Config;
$variable= Config::get('file1.EMP_DOC_PATH');

By this way you can get the variables value from config. I think this will make something good.

use

config('file1.keyname');

if you have made any changes in config file then it might not work . so after changes in config file you have to run the following two commands .

php artisan config:cache

php artisan config:clear

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