How to Share Data in Partial Views using Laravel View Composer

How to Share Data in Partial Views using Laravel View Composer

Have you ever been in a situation, where you have to share the same data with multiple views in Laravel application?

Surely, you can pass the data using controllers to views, but duplicating the same thing in multiple controllers make your application messy and it’s against the DRY principle.

Here come Laravel View Composer to rescue you from such a situation.

So What is Laravel View Composer?

Laravel View Composers are like callback functions, which will run a particular view is rendered. You can attach some data to this callback when a view is requested.

For example, if you have a navigation in your application and you need to pass all menu items from database to a navigation view for the whole application.

You can bind Laravel View Composer to your navigation view which will fetch data from the database and attach to it.

In this scenario, you can attach your navigation view to the whole application which will provide the same menu across the whole site, no matter where you are in your application.

Passing Data to View

You can pass data to view using one of the following methods.

1. Passing data using with() method.
public function index()
{
    $menuItems = Menu::where('active', '1')->get();
    return view('partials.navigation')->with('menuItems', $menuItems);
}
2. Passing data using the second argument in the view helper.
public function index()
{
    $menuItems = Menu::where('active', '1')->get();
    return view('partials.navigation', ['menuItems' => $menuItems]);
}
3. Passing data using compact() method by key-value pairs.
public function index()
{
    $menuItems = Menu::where('active', '1')->get();
    return view('partials.navigation', compact('menuItems'));
}

All the above methods are good to pass data, but things might get messy if you have to repeat the same in all controllers. To overcome this problem, we use Laravel View Composer.

Setting Up Laravel View Composer

Let’s see an example, where we will add menu items to navigation view, it will be a partial view, which will be included in your main layout template.

There is no View Composer folder by default in Laravel, you can save it anywhere within app folder. Laravel recommends to create an app/Http/ViewComposers folder to store all your View Composers.

Creating NavigationViewComposer

This view will populate navigation partial view with menu items from the database.

namespace App\Http\ViewComposers;

use App\Models\Menu;

class NavigationViewComposer
{
    public function compose($view)
    {
        $view->with('menuItems', Menu::where('active', '1')->get());
    }
}

Now run php artisan make:provider ComposerServiceProvider to generate a service provider in app\Providers folder.

Register Your ComposerServiceProvider

Like any other provider add ComposerServiceProvider class to providers array in config/app.php file, so Laravel can resolve this class.

'providers' => [
    ...
    App\Providers\ComposerServiceProvider::class,
]

In order to run our View Composer, we need to register it in boot() method of ComposerServiceProvider class.

namespace App\Providers;

use App\Models\Menu;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        // You can use a class for composer we created earlier
        // you will need [email protected] method
        // which will be called everythime partials.nav is requested
        View::composer(
            'partials.navigation', 'App\Http\ViewComposers\NavigationViewComposer'
        );

        // Or you can use below callback function
        View::composer('partials.navigation', function ($view) {
            $view->with('menuItems', Menu::where('active', '1')->get());
        });
    }
}

You can use one of the above methods, the first one is by using a dedicated class in our case,NavigationViewComposer or you can pass a Closure which is demonstrated in the above code example.

Note: Make sure to provide the full path of your view in View::composer('partials.navigation') to resolve it, in this case, we are passing a navigation.blade.php view file which is located inside resources/views/partials folder.

Passing Data to Multiple Views

You can use an array of views to pass the same data to all views if needed like below.

View::composer(['partials.navigation', 'partials.top-nav', 'partials.footer'])

The handler will pass data to all of three views if you want to pass data to all of the views in your application you can use * wildcard. Like below

View::composer('partials.*')

Laravel View Composer makes your views more independent since data required by the view can be available by View Composers instead of controllers. This will make your code cleaner and maintainable.

You May Also Like

About the Author: WebWhistles

Leave a Reply

Your email address will not be published.