Skip to content
Advertisement

how do I return data from controller to @include in Laravel?

I have a view called side.balde.php which is like a sidebar. This side is called in to home and various other views using @include.

If i just return view side.balde.php with variables/data in my controller, then home view can’t recognize the variables.

 return view('incs/side',compact('users','etc'));

I can return this data with home view and all the @includes in home view can inherit this data. but the problem is I’m using this @include in few other pages.

how do I forward data to side.balde.php so all views calling side.balde.php can access the data.

Advertisement

Answer

there are 2 options to share data across multiple views:

  1. App Service Provider (Short) Open your app service provider and in boot method write:

    $users = User::get(); View::share(‘users’, $users); Now all of your views will have access to that key users.

  2. View Composers Please visit this link to understand how the view composer works.

You can get your work done with the first option though. Thanks.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement