問題:php
使用laravel5.8,前臺全部頁面須要共用標籤數據html
解決方法:laravel
1. 新建ComposerServiceProviderapp
php artisan make:provider ComposerServiceProvider
2. 編輯ComposerServiceProvider中都boot方法,添加composer
view()->composer('home.*','App\Http\ViewComposers\BaseInfoComposer');
說明:全部前臺模板在home文件夾下,因此home.*ide
namespace App\Http\ViewComposers; use Illuminate\Http\Request; use Illuminate\View\View; use App\Models\Tag; use App\Models\Link; class BaseInfoComposer { public function compose(View $view) { $tags = Tag::where('id','>',0)->orderBy('id','DESC')->get()->toArray(); $links = Link::where('id','>',0)->orderBy('id','DESC')->get()->toArray(); $view->with([ 'tags' => $tags, 'links' => $links, ]); } }
至此完工,前臺全部模板以及模板調用都組件均可以使用$tags和$links變量spa
轉載於:https://www.cnblogs.com/convincee/p/10846158.htmlhtm