How to Define Constant in Laravel 8

How to Define Constant in Laravel 8

Hello friends, Today we will learn How to Define Constant in Laravel 8 application. In this post to you can easily define constant in laravel 8 application. you can easily define global variable in laravel 6, laravel 7 and laravel 8 application.

You can define constants variable with string value, integer value, array value and you can access for all controller, all views, all blade files, middleware too in laravel 8 using config.

Read Also :  Laravel Migration Add Enum Column Example

In this example we will create one global config file will all define default variable with values. so you can use that variable from anywhere in project. you can also check bellow screen shot for file location.

Create Config File for Define Constant in Laravel 8

config/global.php

<?php
   
return [
  
    'pagination_records' => 10,
  
    'user_type' => ['User', 'Admin'],
]
  
?>

Use Constant Variable

You can easily get value using config() helper. you can see bellow example:

Route::get('get-user-type', function()
{
    dd(config('global.user_type'));
});
Route::get('get-user-type', function()
{
    dd(config('global.pagination_records'));
})

I hope it can help you…

Read Also :  Laravel 8 Clear Cache of Route, View & Config