How to pass data to partial view file in laravel

Partial view file is a common file which is used to include in all others layout and individual files in laravel. So here we have the problem of passing the data to partials file by a controller method because we can’t pass data to partials by calling the same code again and again in the controller's method. Which is not a good practice so here we use the AppServiceProvider to do so.

Answers 1
  • Passing data to partials/common file using AppServiceProvider in laravel

    public function boot()
        \View::composer('partials/header', function ($view) {
            $userDetails= DB::table('users')->select('id','name','email')->first();
    {{ $user->name }}

    You can pass data to the partial file using appserviceprovider and can display it in the partial view file. Now you can include the partial file in your layout and individual file.

  • Back to code snippet queries related laravel