loading...

Laravel Set Global Variable In Middleware, All Controller And Views

pakainfocom profile image pakainfo ・1 min read

In this example we will create one global config file will all set & get default variable with values. so you can use that variable from anywhere in your project. you can also check bellow config/global.php for file location. Bellow is sample Laravel Global Variable file for config File.

Define Global Variable In Laravel

config/global.php

 '5.8',
    'website_type' => ['eCommerce', 'Business', 'Entertainment', 'Portfolio', 'Media', 'Brochure'],
    'role_list' => ['Media Manager', 'Designer', 'Developer', 'Manager', 'Business Manager', 'Other'],
    'admin_email' => 'admin_email@domain_name.extentions',
    'email_from_name' => 'YOUR Comany Name',
    'notification_email_main' => 'notification_email_main@domain_name.extentions',
    'notification_email' => 'notification@domain_name.extentions',
    'transactions_email' => 'transactions@domain_name.extentions',
    'info_email' => 'info@domain_name.extentions',
];

Use Global Variable

You can simply get & defing value using config() helper. you can see bellow example:
routes/web.php

Route::get('get-website-type', function()
{
    dd(config('global.website_type'));
});

10+ ways to get & set Laravel Global Variable

Posted on by:

pakainfocom profile

pakainfo

@pakainfocom

My name is Angular King. I'm a full-stack developer, entrepreneur and owner of Pakainfo. I live in India and I love to write

Discussion

pic
Editor guide