How to Use Constant Variable in Laravel?

Spread the love

Hellow developers,
In this tutorial we will learn how to define a constant variable in the Laravel 7/6 application. We can easily create a constantly variable Laravel 7/6. You can define constant variables with string value, integer value, array value and access all controllers, all views, all blade files and middleware in Laravel 7/6 with config.

We should be obliged to define a global value for the application, such as B. the number of pagination records, the user type, the site url, etc. If you are working on a small project, I recommend that you create a global configuration file to define the constant variable.

In this example we are creating a global configuration file that defines the default variable with values. So you can use this variable from anywhere in the project. You can also refer to the following screenshot to see the location of the file.

Create configuration file

We need to create a global.php configuration file with a constant variable value, and you can also define the array value like this:

config/global.php

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

Use a constant variable

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

routes/web.php

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 could help you …

thaaaank’s friends ….