How to pass data to multiple partial view files in laravel

Updated at 21-Sep-2021, By samar

How to pass data to multiple partial view files in laravel

We’ll attempt to use programming in this lesson to solve the "How to pass data to multiple partial view files in laravel" puzzle.

You can pass data to multiple partial view files in laravel using appserviceprovider. it returns the data to the specified view files in which you want to show data.
  • Pass data to multiple partial view files using AppServiceProvider in laravel

    //app\Providers\AppServiceProvider.php
    public function boot(){
        
        \View::composer(['partials/right_sidebar', 'partials/left_sidebar'], function ($view) {
            $user = DB::table('users')->select('id','name','email','image')->first();
            $view->with(['user'=>$user]);
        });
    }
      
    //resources\views\partials\left_sidebar.blade.php
    {{ $user->name }}
    //resources\views\partials\right_sidebar.blade.php
    {{ $user->name }}
    

    This code snippet will return the user detail to both left_sidebar and right_sidebar partial view file. You just have to create the file right_sidebar and left_sidebar in resources\views\partials\ directory and display data using the $user variable.

Back to code snippet queries related laravel