laravel boot()服务:无法访问变量

In my application Navigation is compiled via Laravel ServiceProvider compose() method:

view()->composer('partials.nav', function($view) {
    $navData = // retrieve categories from DB... 
    $view->with($navData);
});

I do receive the variable $navData to my nav.blade.php and compile navigation successfully. However some of the data passed to navigation I want to use on a page that @includes the navigation, for example page.blade.php:

@include('partials.nav')

<div id="main">
{{ $navData['category'] }}
</div>

But I throws error: Undefined variable: $navData. Is there a way to access this variable outside the nav.blade.php without making any additional calls?

You're using $navData in page.blade.php, not in partials.nav. So to make it work replace

view()->composer('partials.nav', function($view) {

with

view()->composer('page.blade.php', function($view) {

or

view()->composer('*', function($view) {

if you want to make $navData available everywhere.

use this

view()->composer('partials.nav', function($view) {
$data['navData'] = // retrieve categories from DB... 
$view->with($data);
});