AppServiceProvider中所有视图使用的Laravel变量



我想知道是否有一种方法可以与AppServiceProvider中的所有视图共享收藏。我有几个页面使用的几个视图,它们都需要为用户提供邮政编码和相关的地理位置信息。与在每种观点中运行此查询相比,我希望有一种更简单的方法,不幸的是,可能没有成功。我发现与视图的全球共享相关的所有内容,而不是在AppServiceProvider中,这是我需要使用的。我希望$ t1在所有视图中可用。感谢您的帮助!

    view()->composer('common.new.restaurants', function($view)
    {
        $t1=loc_zip::where('zip',auth()->user()->last_zip)->first();
        $view->with(['r'=>$r]);
    });

    view()->composer('common.categories.restaurants', function($view)
    {
        $t1=loc_zip::where('zip',auth()->user()->last_zip)->first();
…
            $view->with(['r'=>$r,'a'=>$a]);
        });

是的,如下所述,https://laravel.com/docs/5.4/views#sharing-data-with-with-all-views

这篇文章更深入地讨论了Laravel 5,View :: Share

最新更新