The @once Blade Directive
Render a block of template content only once per rendering cycle, even inside a loop.
Overview
The @once directive ensures a block of Blade content is only rendered a single time, no matter how many times the surrounding template is evaluated.
Usage
@foreach ($entries as $entry)
@once
<script src="/js/entry-widget.js"></script>
@endonce
<div class="entry">{{ $entry->title }}</div>
@endforeach
When to Use
- Including a script or stylesheet inside a loop-rendered component
- Adding a one-time setup block inside a reusable partial
- Preventing duplicate resource loading in nested components