Method 1: Share via boot function in AppServiceProvider
The first method is to share the variable via the "boot()" function in AppServiceProvider and by passing your data like the following it will be shared across all the views.
<?php // App\Providers\AppServiceProvider public function boot() { view()->share('key', 'value'); }
Method 2: View Composer
The next method is to use view composer and you can define your code like below. This will ensure that it's shared to all views "*" and the data that's being passed on for this example is the "posts"'.
<?php view() ->composer('*', function ($view) { $posts = Post::take(10)->get(); $view->with('posts', $posts); });
Leave a reply