Laravel 在 ViewServiceProvider 上多次查询
Laravel Query Multiple times on ViewServiceProvider
我使用 view->with()
将我的全局变量放在 ViewServiceProvider.php 上,但问题是它在单个页面上查询多次。这是一个问题还是真的只是这个?它在
下面多次显示 select * from weather where id = 1
是的,所以这里的问题是因为您正在为视图编辑器使用 *
,这意味着您要为正在呈现的每个视图执行此代码。更好的方法是将它包含到使用 weather
实例的部分中,例如:
view()->composer('partial.nav', function($view) {}); // this will be used only on the navigation partial view.
// multiple views like this:
view()->composer(
['profile', 'dashboard'],
function($view) {}
);
// or create a view composer class and register it as a singleton
$this->app->singleton(\App\Http\Composers\WeatherComposer::class);
我使用 view->with()
将我的全局变量放在 ViewServiceProvider.php 上,但问题是它在单个页面上查询多次。这是一个问题还是真的只是这个?它在
select * from weather where id = 1
是的,所以这里的问题是因为您正在为视图编辑器使用 *
,这意味着您要为正在呈现的每个视图执行此代码。更好的方法是将它包含到使用 weather
实例的部分中,例如:
view()->composer('partial.nav', function($view) {}); // this will be used only on the navigation partial view.
// multiple views like this:
view()->composer(
['profile', 'dashboard'],
function($view) {}
);
// or create a view composer class and register it as a singleton
$this->app->singleton(\App\Http\Composers\WeatherComposer::class);