Home / Snippets / How to Push Stacks Only Once in Laravel
How to Push Stacks Only Once in Laravel cover

How to Push Stacks Only Once in Laravel

1.3K

3 years ago

0 comments

If you face an issue where you are pushing multiple styling or scripts in a single blade file, you can prevent this by wrapping the "stack" with the "once" directive.

The "once" directive will only evaluate the stacks once per rendering cycle which will ensure that there won't be code duplicate.

Laravel Once Directive
For javascript/js stacks you can define it like this.
@once
    @push('js')
        <script>
            console.log('hello world');
        </script>
    @endpush
@endonce

For styles/CSS stacks it's pretty much the same.
@once
    @push('css')
        <style>
            .card { background-color: pink; }
        </style>
    @endpush
@endonce
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this