How to pass data to all views in Laravel 5?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Simplifying Data Sharing Across All Views in Laravel 5: A Comprehensive Guide Body:

If you're working on a Laravel 5 application, you might encounter situations where you need to share data with all your views. This can be done using the Laravel view composer and by sharing specific variables or arrays of data. Let us dive into how to accomplish this task efficiently.

View Composer

To share the same default data across all views, you first need to set up a view composer. A view composer is a class that's responsible for sharing and updating variables or arrays before your application renders any view. The composer can be found under app/Providers/RouteServiceProvider.php:

namespace App\Providers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Routing\Router;

class RouteServiceProvider extends \Illuminate\Routing\RouteServiceProvider {
    public function boot(Application $app, Router $router) {
        parent::boot($app, $router);

        View::composer('*', function ($view) {
            // Your code to share data with all views goes here.
        });
    }
}

In this example, the boot() method is responsible for loading the RouteServiceProvider class. The composer function takes two parameters: the view and a closure. The asterisk (*) in the first parameter represents any view. In simple terms, it means all views will be passed through this closure to share the data.

Sharing Data with View::share()

Now that you've set up the view composer, the next step is to use the View::share('data', [1, 2, 3]) method. You can replace 'data' with any name you want for your data and the array containing example values. This code snippet will create a new variable called 'data' in your application's shared data container.

Accessing Shared Data from Views

To access this shared data, simply call it by its name within the views, such as {{ $data }}. This will display all of the values stored in the 'data' array. You can also use a loop to iterate through each item. In case you are using Blade templates, remember to include the proper tag like @foreach($data as $value).

Expand on View Composer

The view composer can be extended for more complex functionalities. For example, it's possible to share different data sets based on the current route or controller. You can also use the 'shared' and 'private' closures within your composer to share data across all views or solely with specific ones. Refer to Laravel's documentation here for more detailed examples of view composition.

Conclusion

Now that you have a better understanding of how to pass data to all your Laravel 5 views using the view composer and View::share(), you can easily set up default data for every single view. Always keep in mind the best approach for sharing data based on your project's requirements, as not every situation will be the same.

Further Reading and Resources:

- Share Data With All Views in Laravel by Laravel Company - Laravel Docs on View Composer