Pass values to all views in laravel 5.4

Pass values to all views in laravel 5.4

View Composer in laravel .I have one table called site_settings which have all the global data about site. For Eg, Site name, Logo, Fav Icon, Copy Rights, Google Analytics etc.I want to use these
datas in view file where ever I want. I cannot run query for each time when even I need these details. In laravel we can globally assign the variable and data to access it in all view files. Lets see how to achieve it. We can achieve it by Laravel using custom service provider.

Step 1 : Create Service provider.

You can keep any custom name. In my case I have named as ViewServiceProvider.
Run below command to make a provider :

It will create ViewServiceProvider.php under app/Providers folder. Its Looks likes below

Step 2 : Add global variable to access

Here inject your Model and Add Model values to custom variable

In My case my Model is App\Models\SiteSetting;
My custom Variable is global_site_setting
view()->share(), //It will share with all views.

Step 3 : Add your service provider in config/app.php provider array

Step 4 : Access your custom variable in where you need


Lets see How share custom variable for particular file. You need to right ViewServiceProvide.php at boot() function

Eg :

I am going to share custom variable only for : resources/views/admin/index.blade.php


Thats it. Now you can access your variable globally.

Thanks for reading. Hope it will help someone.