How To Define Global Constant Variables in Laravel 10

Share this Article
Reading Time: 3 minutes
128 Views

Inside this article we will see the concept of How To Define Global Constant Variables in Laravel 10. Article contains classified information about Creating and Using Global Constants 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 10 global or constant variables.

Read More: How To Create Custom Blade Directive in Laravel 10 Tutorial

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.

Read More: How To Create Custom Helper Function in Laravel 10 Tutorial

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 Global Constant Variables in Laravel 10 in a very detailed way.

Read More: How To Create Custom Log File in Laravel 10 Tutorial

Buy Me a Coffee

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.