How to Declare Global Variable in Laravel 6?

Today, We want to share with you How to Declare Global Variable in Laravel 6?.In this post we will show you laravel 6 set global variable in middleware, hear for Laravel 6 define global constants Config php file we will give you demo and example for implement.In this post, we will learn about how to make a global variable in laravel 6 controller with an example.

How to Declare Global Variable in Laravel 6?

There are the Following The simple About How to Define Global Variable in Laravel 6.0 Application Full Information With Example and source code.

As I will cover this Post with live Working example to develop How to define Global or constant Variables in Laravel 6?, so the Define Global Variable in Laravel 6.0 is used for this example is following below.

Step 1: Create a Laravel 6 Global Config File

config/global.php

 555,
	    'product_type' => ['Simple', 'Grouped', 'Varible'],
	]
?>

Step 2: Use Laravel 6 Global Variable

We can simple way to get/fetch value using config() main laravel global helper. we can see bellow laravel Route example:

routes/web.php

Route::get('get-product-type', function()
{
    dd(config('global.product_type'));
});
Route::get('get-product-type', function()
{
    dd(config('global.total_products'));
});

global variable for all controller and views

class BaseController extends \Controller
{

    protected $product_type;

    public function __construct() 
    {
        // Fetch the All Product object
        $this->product_type = Product::all();
        View::share('product_type', $this->product_type);
    }

}
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about super global variable in laravel 6.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment