How to Define Constant Variable in Laravel

How to Define Constant Variable in Laravel

In this post we will give you information about How to Define Constant Variable in Laravel. Hear we will give you detail about How to Define Constant Variable in Laravel And how to use it also give you demo for it if it is necessary.

As you may already be aware, the popularity of the Laravel framework is growing as a result of its practical features and myriad time-saving tricks.

Building global variables that can be utilized across your project is frequently important. For example, you may specify them in a single file and use them everywhere you need them instead of manually passing them if you are working in the admin section and need to submit one record per page to paginate() method to expose on record at a time everywhere you go.

There is a method for defining constants in a .env file that we are familiar with, but is there another choice, such as putting all constants in a constant file? If you use constants frequently and most of them are used throughout the program, you should keep them in a global.php file. For this method, just create a file called global.php in the config directory. An array of configuration values from the file must be returned.


How to Define Constant Variables in Laravel

1. Create a config file

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


return [


2. Use Constant Variable

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


Route::get('/users', function(){

    return config('global.pagination.perPage');

Also see: How to get the raw SQL query from the Laravel Query Builder

Hope this code and post will helped you for implement How to Define Constant Variable in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US