X
    Categories: Laravel

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 :

php artisan make:provider ViewServiceProvider

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

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
class ViewServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

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.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Models\SiteSetting;
class ViewServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->share('global_site_setting', SiteSetting::find(1) ); 
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

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

App\Providers\ViewServiceProvider::class

Step 4 : Access your custom variable in where you need

{{ $global_site_setting }}

Eg:

Site name : {{ $global_site_setting->site_name  }}
Site Logo : {{ $global_site_setting->logo  }}

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

Eg :

 view()->composer('admin.index', function ($view)
        {
            $view->with('global_site_setting', [ 'site_name' => 'Your Name', 'logo' => 'Path to Logo'  ] );
        });

 

Thats it. Now you can access your variable globally.

Thanks for reading. Hope it will help someone.

 

Marimuthu:

View Comments

    • Schema::create('site_settings', function (Blueprint $table) {
      $table->increments('id');
      $table->string('title')->nullable();
      $table->string('slogan')->nullable();
      $table->string('meta_title', 500)->nullable();
      $table->text('meta_keyword')->nullable();
      $table->text('meta_description')->nullable();
      $table->integer('language_id')->unsigned();
      $table->integer('currency_id')->unsigned();
      $table->string('logo', 500)->nullable();
      $table->string('fav_icon', 500)->nullable();
      $table->text('google_analytics')->nullable();
      $table->text('copy_rights')->nullable();
      $table->timestamps();

      $table->foreign('currency_id')->references('id')->on('currencies');
      $table->foreign('language_id')->references('id')->on('languages');
      });