How to Define Global Variable in Laravel 5.8 Application

How to Define Global Variable in Laravel 5.8 Application

In this tutorial, we will learn how to define global variable in laravel 5.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 5.8 using config

In this tutorial, we will learn how to define global variable in laravel 5.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 5.8 using config

We must be require to define some global value for application like number of pagination records, user type, site url etc. if you are working with small project then i will suggest to create global config file for define constants variable.

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 Global Config File

We need to create global.php config file with constants variable value and you can also define array value like as bellow:

config/global.php

<?php

return [

    'pagination_records' => 10,

    'user_type' => ['User', 'Admin'],
]

?>

Use Global Variable

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

routes/web.php

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....

laravel php web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

PHP Web Application Development Company

As a top **PHP Web Application Development Company in USA**[https://www.dataeximit.com/php-development-services/](https://www.dataeximit.com/php-development-services/ "https://www.dataeximit.com/php-development-services/"), we at Data EximIT have...

PHP Website Development

Skenix Infotech is a leading PHP Web Development Company that helps companies get results with industry best practices. Get affordable PHP Development Services.

Laravel Development Company

Skenix Infotech is a top Laravel Website Development Company with Expert Laravel Developers that provides robust Laravel Development Services at fair costs.

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Hire PHP Developer

Looking to develop a PHP based website from scratch or revamp your existing website? **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")** has always been an industry leader for companies and business owners looking to hire...