我使用 compact
从我的 Controller 将变量传递到我的 View 函数,在我看来这是可以访问的。然而,每次我访问这个变量时,Laravel 都会对数据库进行越来越多的调用。
return view('dashboard.overview', compact('aircraft', 'current_location', 'current_flight'));
然后在我的 Blade 中,我调用这样的 Vue 组件:<badge--aircraft--financed :data-aircraft="{{ $aircraft }}"></badge--aircraft--financed>
,这会产生 10 个查询。如果我复制这一行,Debugbar
显示我进行了 20 个查询,其中 10 个是重复的。 Blade 是否不缓存 $aircraft
默认变量?
最佳答案
Laravel 仅缓存 Blade 模板,而不缓存变量。 您可以在 Controller 中缓存变量 https://laravel.com/docs/6.x/cache#cache-usage
此外,为了防止大量查询,您可以在获取 $aircraft 模型时调用 with()
方法。
https://laravel.com/docs/6.x/eloquent-relationships#eager-loading
关于php - 有没有办法在 Blade 中缓存变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58972673/