Laravel Blade Once Component

Laravel Blade Once Component

As of Laravel 7.25, Blade now includes a new @once component that will only render the items within the tags one time. It’s a little hard to explain and much easier to show with an example, and here is one Taylor included in the original pull request:

Component content here...

@once
    <script>
        This will only be included in the template once even if component is rendered multiple times.
    </script>
@endonce

Then Mohamed Said followed up with another example:

@once
<template id="recieptsTemplate">
    <div class="flex flex-column items-center">
        <div class="w-1/2" data-reciept="paid_at"></div>
        <div class="w-1/4" data-reciept="amount"></div>
    </div>
</template>
@endonce

<div data-id="receipts" class="divide-y divide-grey-300"></div>

This component renders a list of receipts. The template for the receipt has to be rendered once in the entire page while the component itself can be used multiple times.

This once feature is now pulled into Laravel starting with v7.25 so you can start using it today.

Filed in: News / Blade

Newsletter

Join 31,000+ others and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Software Developer
Remote (US & Canada)
Alley
😎 Laravel Developer
Remote
Jogg, Inc
Junior, Mid, and Senior Software Engineers. Laravel / Vue. Saint Petersburg, FL
Saint Petersburg, FL and Remote
ShineOn
Senior PHP Developer (Laravel)
Remote
The Interaction Design Foundation
Fullstack Laravel Engineer (Munich/Remote) 💻
Munich, Germany or Remote
AirLST GmbH