How To Define Laravel 8 Global or Constant Variables

Reading Time: 3 minutes
16,737 Views

Inside this article we will see How to work with laravel 8 global or constant variable. This will gives you how to define a global variable in laravel application.

Global or constant variables are those variables which will be define at a place and used anywhere inside application. Entire application will access it and one more thing that is values of those variables can’t be changed.

Tutorial is super easy to understand and easy to follow to implement it. We will see the concept of how to define laravel 8 global or constant variables.

If you surf this topic to internet, you will get several solution to this. But we will see the best option for it as per laravel standard.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Define Global Constant

To define global constants in laravel application we have several ways. But we will see about two:

  • By .env file
  • By Custom Constant file

By .env file

Open .env file and define constant like this:

//...

OWT_APP="ONLINE WEB TUTOR APPLICATION"

//...

To access in application either in blade files or in controller we use helper function of env.

echo getenv("OWT_APP");

echo env("OWT_APP");


By Custom Constant file

Create constants.php inside /config folder.

Open constants.php and write this code into it.

<?php 

return [
	
	'OWT_APP' => 'ONLINE WEB TUTOR APPLICATION',
	'OWT_KEY' =>'xxxxx-xxxx',
	'OWT_SECRET' => 'xxxxxxx-xxxxxx-xxxx'
];

Here, we have a set of values. These values are constants.

Next, we need to access it inside application.

We can access value in two ways –

  • Using in Controller
  • Using in Blade Layout Files

Access Value in Controller

To access in controller files we will use config helper function.

echo config("constants.OWT_APP");

echo config("constants.OWT_KEY");

echo config("constants.OWT_SECRET");

Access Value in Blade Layout File

{{ config('constants.OWT_APP') }}

{{ config('constants.OWT_KEY') }}

{{ config('constants.OWT_SECRET') }}

constants.<Variable_Name> is syntax. constants is the name of file.

We hope this article helped you to learn how to define laravel 8 global or constant variables Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more