Performance: Raw PHP in Blade (@php)
MEDIUM IMPACT
Using raw PHP in Blade templates affects page rendering speed and can increase server processing time, impacting the time to first byte and overall load speed.
// In Controller $total = collect($items)->sum('price'); return view('cart', compact('total')); // In Blade <p>Total: {{ $total }}</p>
@php $total = 0; foreach($items as $item) { $total += $item->price; } @endphp <p>Total: {{ $total }}</p>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Raw PHP in Blade (@php) with logic | Minimal DOM nodes added | No direct reflows | No direct paint cost | [X] Bad - slows server rendering |
| Pre-calculated data passed to Blade | Minimal DOM nodes added | No direct reflows | No direct paint cost | [OK] Good - faster server response |