Reputation: 1868
I only need the latest 5 rows in my query so I use
$view->with('recent', Transaction::SELECT('amount')
->latest()
->where('amount', '!=', null)
->take(5)->get());
This gives me what I need in my view, but the DeBug Bar states:
14 statements were executed, 14 of which were duplicated
I currently have 14 records in that table. The more records I add in the table the more duplicates I get.
How can I cut down on those queries?
Currently calling it like this:
@foreach($recent as $rec)
<li>{{ number_format($rec->amount / 100, 2) }}</li>
@endforeach
Upvotes: 0
Views: 2144
Reputation: 795
try this
change 'change.to.footer' to your footer.blade.php path
change \App\Transaction to your model path
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
view()->composer('change.to.footer', function ($view) {
$view->with('recent', \App\Transaction::SELECT('amount')
->latest()
->where('amount', '!=', null)
->take(5)->get());
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
if you want use the variable in all blade,
may be you can make a model method
// Transaction model
public static function recent()
{
return static::SELECT('amount')
->latest()
->where('amount', '!=', null)
->take(5)->get());
}
make a view composer
public function boot()
{
view()->share('recent', \App\Transaction::recent());
}
Upvotes: 1