Omi
Omi

Reputation: 4007

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?

Upvotes: 8

Views: 32863

Answers (4)

kishan maharana
kishan maharana

Reputation: 557

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

Upvotes: 7

VeRJiL
VeRJiL

Reputation: 564

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');

Upvotes: 5

Sankar Smith
Sankar Smith

Reputation: 356

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.

Upvotes: 3

Sougata Bose
Sougata Bose

Reputation: 31739

Assuming for SUB_KEY2, Try -

config('file1.MASTER_KEY.SUB_KEY2')

Guide

Upvotes: 18

Related Questions