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 and laravel 8 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:
'pagination_records' => 10,
'user_type' => ['User', 'Admin'],
Use Constant Variable
You can easily get value using config() helper. you can see bellow example:
I hope it can help you....
- Laravel 6 Create Custom Helper Function
- Laravel 7/6 REST API with Passport Tutorial
- Laravel 6 Email Verification Tutorial
- Laravel 7/6 Multi Auth (Authentication) Tutorial
- Laravel 6 CORS Middleware Tutorial
- Laravel 6 Multiple Image Upload Tutorial
- Laravel 6 Ajax Form Validation Tutorial
- Laravel 6 Image Upload Tutorial
- Laravel 6 CRUD Application Tutorial