How to Define Constant Variable in Laravel?
In this tutorial, we will learn how to define constant variable in laravel application. we can easily create laravel 7/6 constants variable. you can define constants variable with string value, integer value, array value and you can access for all controller, all views, all blade files, middleware too in laravel 8 using config.
you can easily define global variable in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.
We must be require to define some global value for application like number of pagination records, user type, site url etc. if you are working with small project then i will suggest to create global config file for define constants variable.
In this example we will create one global config file will all define default variable with values. so you can use that variable from anywhere in project. you can also check bellow screen shot for file location.
Create Config File
We need to create global.php config file with constants variable value and you can also define array value like as bellow:
config/global.php
<?php
return [
'pagination_records' => 10,
'user_type' => ['User', 'Admin'],
]
?>
Use Constant Variable
You can easily get value using config() helper. you can see bellow 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 can help you....
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Show Pagination with Serial Number in Laravel?
- How to Generate a Dropdown List of Timezone in Laravel?
- How to use Bootstrap Icons in Laravel Vite?
- How to Install Sweetalert2 in Laravel 10 Vite?
- Laravel Country List with Flags Example
- How to Show Data in Modal using Ajax in Laravel?
- How to Use and Install Font Awesome Icons in Laravel?
- How to Install JQuery UI in Laravel Vite?
- How to Read Content from PDF File in Laravel?
- Laravel Password and Confirm Password Validation Example
- Laravel Remove All Spaces from String Example
- Laravel withSum() with Where Condition Example
- Laravel Eloquent Order By Length Query Example