How To Define Laravel 9 Global or Constant Variables

Share this Article
Reading Time: 3 minutes
3,601 Views

Inside this article we will see How to work with laravel 9 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 9 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

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

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 define these constants 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

To access in controller files

echo config("constants.OWT_APP");

echo config("constants.OWT_KEY");

echo config("constants.OWT_SECRET");

To access inside blade files

{{ 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 9 global or constant variables Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5