Laravel Advance | Define Global Variable in Laravel

Raviya Technical
1 min readJan 31, 2022

In this tutorial, we will learn how to define global variables in the laravel 5.8 application. you can define constants variable with a string value, integer value, array value and you can access for all controllers, all views, all blade files, middleware too in laravel 5.8 using config.

We must be required to define some global value for an application like a number of pagination records, user type, site URL, etc. if you are working with a small project then I will suggest creating a global config file for defined constants variable.

In this example, we will create one global config file that will all define the default variables with values. so you can use that variable from anywhere in the project. you can also check the below screenshot for the file location.

Create Global Config File

We need to create a global.php config file with constants variable value and you can also define array value like as bellow:


<?phpreturn ['pagination_records' => 10,'user_type' => ['User', 'Admin'],]?>

Use Global Variable

You can easily get value using config() helper. you can see the bellow example:


Route::get('get-user-type', function(){dd(config('global.user_type'));});Route::get('get-user-type', function(){dd(config('global.pagination_records'));});

I hope it can help you….