How to share data across multiple views in Laravel

Kiptoo Korir
3 min readNov 3, 2021

--

A laptop computer with a code editor opened up in full screen
Photo by Mohammad Rahmani on Unsplash

Let’s say you are working on a Laravel application and at some point you end up needing the user information in some of your views and as such there’s a section of code that is repeated whenever you are working on a new view. You might export the functionality to a service to enable re-use but Laravel has a much better solution to handle the issue.

In this tutorial we will discuss how to use view composers to share data across views that are rendered within your Laravel application.

What are view composers, you may ask. They are callbacks or class methods that are executed when a view is rendered. Whenever you need some data to be bound to a view each time it is rendered, composers come in handy and allow the placement of your logic in a single location.

By the end of the tutorial, you will be able to share your authenticated user’s data across multiple views. Let’s get started with the following simple steps:

Step 1: Create a view composer

View composers can either be callback functions or class methods. In this tutorial we will use the class methods as it will be more readable and relatable to a real world problem.

Create a new directory in your app folder which will name Views. In this directory, create another new directory named Composers. In this folder create a php class named UserDataComposer.

<?phpnamespace App\Views\Composers;use Illuminate\Support\Facades\Auth;
use illuminate\View\View;
class UserDataComposer { public function __construct() {
}
// The compose function here handles the logic of binding data to the view public function compose(View $view) {
$user = Auth::user();
// With method accepts two arguments, a key and a value $view->with('userInfo', $user)
}
}

The class will have just the compose method which will handle logic for attaching data to the view. In case of multiple values that need to be bound to the view, an associative array of key-value pairs can be passed in as a single parameter to the $view->with() method

Dependencies can also be added to the class and they will be automatically resolved by the service container. As such there will be no need of adding said dependencies as parameters when using the composer class. In case of dependencies,

public function __construct(UserDataService $userDataService){ 
// Dependencies are automatically resolved by the service container
$this->userDataService = $userDataService;
}

Step 2: Creating a new service provider

For view composers to work as desired, they need to be registered within a service provider. Open up a new terminal in your project directory create a new one.

php artisan make:provider ShareDataServiceProvider

Step 3: Attach your composer to multiple views

Go ahead to use your composer in the new service provider.

<?phpnamespace App\Providers;use App\Views\Composers\UserDataComposer;
use Illuminate\Support\ServiceProvider;
class ShareDataServiceProvider extends ServiceProvider {
/**
* Register services.
*/
public function register() {
}
/**
* Bootstrap services.
*/
public function boot() {
view()->composer(['home', 'profile'], UserDataComposer::class);
}
}

Step 4: Register your service provider in app.php

With steps one through three completed, register the new service provider in the app.php file in the config folder under the Application Service Providers list.

// Application Service Providers...
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ShareDataServiceProvider::class,

With that you should be able to access the user’s information in the home and profile views.

Find out more about Laravel’s view composers in Laravel’s docs. https://laravel.com/docs/8.x/views#view-composers

--

--

Kiptoo Korir
Kiptoo Korir

Written by Kiptoo Korir

Web Developer | Information Geek | Occasional Poet - I talk about software development and life in general.

No responses yet