logeeks
logeeks

Reputation: 4979

Storing key/value pairs globally in Laravel 8.x

I need to access the NAME of the languages and their respective ID from a global array. I have added PHP files under the config directory and added the following code. Is it the right way to do it? How can I access it in my controller file?

return [
    'Language' => [
        array( 
            "English" => array (
               "name" => 'English',
               "value" => 101
               
            ),
            "Turkish" => array (
               "name" => 'Turkish',
               "value" => 102
               
            ))
    ]
];

Upvotes: 0

Views: 562

Answers (1)

Donkarnash
Donkarnash

Reputation: 12835

Create a file config/language.php with the contents

///config/language.php

return [ 
    "English" => [
        "name" => 'English',
        "value" => 101
               
     ],
     "Turkish" => [
         "name" => 100,
         "value" => 101
               
     ]
];

Then anywhere your need you can access like

$lang = config('language.Turkish');

$lang['name'] //will be 100
$lang['value'] //will be 101

Upvotes: 2

Related Questions