user11974734
user11974734

Reputation: 37

How to pass data to multiple blades in laravel and with a single route

i have tried to passing data to multiple blade in controller but get error. Here bellow my code

public function index()
{
    $news = DB::table('beritas')
            ->select('id','judul_berita','created_at')
            ->get();

    return view (['berita.daftar-berita', 'more-menu.berita'])->with(compact('news'));
}

How to pass data to multiple blades in laravel and with a single route?

Upvotes: 0

Views: 1299

Answers (2)

Sumit Kumar
Sumit Kumar

Reputation: 1902

Probably the right place to this is in the boot method of some service provider, for example, AppServiceProvide.

//AppServiceProvider.php 

public function boot()
{
   view()->share('someVariable',$someVariable);

}

This will make someVariable available to all of blade views. This is useful for template level variables.

Upvotes: 0

Abdes
Abdes

Reputation: 986

If u want pass data to multiple blades u can share it in the Constructor like so:

public function __construct(){

      $this->middleware(function ($request, $next) {

           $news = DB::table('beritas')>select('id','judul_berita','created_at')->get();
           View::share('news', $news);
           return $next($request);
        });

    }

and now u can use news variable in all you blades that using the same controller.

i hope it's will help you

Upvotes: 1

Related Questions