Easy to add CONSTANT in laravel

  • September 18, 2019
  • Laravel / php
Easy to add constant in laravel

Hello, today we are going to talk about constant in laravel.

so basically what is constant, i hope you know this but if you don’t know keep read this post.

Constant is the value that’s never getting change during the program execution.

We know that in laravel .env file in root is contain value in file which we can define constants, but other than that is there any method or way to storing all constants in a constant file?.

Yes, we can do it by storing constant in one file and use that in overall system where you want it to use.

Let’s see. If you are using a lot of constants and most of them are used globally in the application storing the constants in a constant.php file is a simple and good option.

For this method just create a file say filename.php (suppose my file name is constants.php, you can use anything here) in the config directory. In the file you have to return array of config values.

return [

'options' => [
	'name' => 'EcodeBlog',

	'email' => 'admin@ecodeblog.com',

	'name' => 'Ecodeblog - The techBlog',

	'path' => 'asset/public/',


Access these constants as shown below.

Config::get('constants.options'); // to fetch all options constant value

While get constant value remember you have to write whatever your “filename.array_element.element” like Config::get('filename.arrayname');

Or wan to fetch single specific key from constant

Config::get('constants.options.name'); // get application name

While get constant value remember you have to write whatever your “filename.array_key.element” like Config::get('filename.arrayname.keyname');

In this way you can either store constant value in variable or directly echo this variable to see the value.

I hope now you clear with CONSTANT in laravel framework.

Thank you for reading. 🙂

Leave a Reply