Posts Learn Components Snippets Categories Tags Tools About
/

How to Push Stacks Only Once in Laravel

Learn how to push your styling or scripts inside stacks once only to prevent code duplication

Created on Jul 03, 2021

1148 views

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

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

Load comments for How to Push Stacks Only Once in Laravel

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours
)