What are Laravel View Composer?

Sometimes we need to pass similar type of data to different view files or partials and later changing them is a pain point as you have to change every single one of them, hence to fix this issue view composers are used.

First we have to open AppServiceProvider.php in app/Providers.

Add View facade to the service provider

use Illuminate\Support\Facades\View;

After adding View Facade add composer code to boot function

function boot()
       View::composer('page.index', function($view){
            $view->with('data', 'Value This could output of eloquent too')


  • page.index is the location of targeted blade file in view folder
  • You can also use wildcard to target all blade file in a directory (page.*)
  • To target more than one file you can pass array instead of string.

Leave a Reply

Your email address will not be published. Required fields are marked *