How To Define Laravel 8 Global or Constant Variables

Share this Article
Reading Time: 3 minutes
617 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Define Global Constant

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.

Buy Me a Coffee

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.

Find More on Laravel 8 Articles here