Solving Laravel 7's Global Variable Puzzle

Set Global Variable using share method in ServiceProvider Laravel 7

Set Global Variable in ServiceProvider - Laravel 7

In this article we will learn how to set global variable in Laravel 7 project . It's very easy to implement . We will see how can you set a global variable for whole project with example . We will share method provided by Laravel for making a variable Global using ServiceProvider .

Basically the purpose of making a global variable that you can access that variable throughout your project like inside all view files controllers etc . So that you don't need to define the same variable in each of you files .

How it Works :

Whenever you runs your Laravel Project , it automatically executes all your provider classes and these classes are automatically accessible throughout the project . So to make a global variable for your project follow the steps given below .

You can refer the Official site also to know more about ServiceProvider .

Step 1 - Create a ServiceProvider : 

First you need to create a new ServiceProvider or you can also use use Default ServiceProviders present in " project_name/app/providers " , i will recommend to create a new one . Use the following command to create a new ServiceProvider .


php artisan make:provider provider_name

php artisan make:provider TestServiceProvider

app/providers/TestServiceProvider :


<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
         //

    }
}


Step 2 - Register ServiceProvider :

After creating ServiceProvider , we need to register our service provider inside " project_name/config/app.php " . Open your config/app.php file and inside the " providers " array register your provider as shown in the following - 


 'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        // App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        Laravel\Socialite\SocialiteServiceProvider::class,
        Intervention\Image\ImageServiceProvider::class,

        // This the example ServiceProvider

        App\Providers\TestServiceProvider::class,


    ],


Step 3 - Making Global Variable :

The next step is to make a global variable inside ServiceProvider . To make a global variable open you ServiceProvider you made and write the following code inside your "  boot method " as shown in the following .


public function boot()
    {
         view()->share('name', 'studywithkishan.online');

    }

The first parameter inside the "  view->share( parameter1 , parameter2 ) " method is the variable name and the parameter2 is the value of the variable . 

Step 4 - Output :


{{$name}}


Important Note : 

While using the variable it might show you and error that " variable is not defined " . use the following command to overcome such error .


php artisan config:cache
Previous Post Next Post

Contact Form