A practical example about how to use the new Blade @once Directive
, available from Laravel 7.25 version.
According to the Laravel documentation
The @once
directive allows you to define a portion of the template that will only be evaluate once per rendering cycle. This may be useful for pushing a given piece of JavaScript into the page's header using stacks. For example, if you are rendering a given component within a loop, you may wish to only push the JavaScript to the header the the first time the component is rendered:
@once @push('scripts') @endpush @endonce
How to use may be a little confusing.
Hereafter a practical example.
Imagine you need to include multiple times a blade component that create carousel on your website page.
With the @once Directive
the carousel init script will be included just one time at the end of your page.