How to Use View composer in Laravel


Laravel is a popular PHP framework known for its simplicity and elegance. One of the key features of Laravel is the View composer, which allows you to share data with multiple views. In this blog article, we will explore how to use View composer in Laravel and leverage its power to enhance the user experience.

What is View composer?

View composer is a technique in Laravel that enables you to bind data to a view or a group of views. It allows you to separate the logic of fetching and preparing data from the view itself, making your code more organized and maintainable. With View composer, you can define callbacks that are executed when a view is rendered, and these callbacks can fetch the necessary data and pass it to the view.

Using View composer in Laravel

To use View composer in Laravel, you need to follow the steps below:

Step 1: Create a View composer class

First, you need to create a View composer class. This class will contain the logic to fetch the data that you want to share with the view. You can create this class anywhere in your Laravel application, but it’s recommended to create it in the app/Http/View/Composers directory. Let’s say we want to create a View composer for the sidebar of our blog. We can create a class named SidebarComposer:

namespace App\Http\View\Composers;

use Illuminate\View\View;
use App\Repositories\PostRepository;

class SidebarComposer
    protected $posts;

    public function __construct(PostRepository $posts)
        $this->posts = $posts;

    public function compose(View $view)
        $view->with('recentPosts', $this->posts->getRecentPosts());

In the above code, we are injecting an instance of the PostRepository class into the constructor of the SidebarComposer class. This repository class will handle the fetching of recent posts. The compose method is where we bind the data to the view. In this case, we are passing the recentPosts variable to the view.

Step 2: Register the View composer

Once you have created the View composer class, you need to register it in your Laravel application. You can do this in the boot method of the AppServiceProvider class, which is located in the app/Providers directory. Open the AppServiceProvider class and add the following code:

use App\Http\View\Composers\SidebarComposer;
use Illuminate\Support\Facades\View;

public function boot()
    View::composer('partials.sidebar', SidebarComposer::class);

In the above code, we are using the View facade to register the SidebarComposer class with the partials.sidebar view. This means that whenever the partials.sidebar view is rendered, the SidebarComposer class will be invoked, and the data will be passed to the view.

Step 3: Use the shared data in the view

Finally, you can access the shared data in the view. In our example, we can access the recentPosts variable in the partials.sidebar view like this:

@foreach ($recentPosts as $post)
    <div>{{ $post->title }}</div>

In the above code, we are iterating over the recentPosts array and displaying the title of each post.

Now include the posts in any view blade file and it will show the posts.



Using View composer in Laravel can greatly improve the organization and maintainability of your code. By separating the logic of fetching data from the view, you can make your code more modular and reusable. In this blog article, we have covered the basics of using View composer in Laravel and provided an example of how to share data with a view. Now, it’s your turn to explore this powerful feature and take your Laravel development to the next level!

Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x