Blade Component With Javascript
This is a snippet from Jeffrey Way but he has deleted the original tweet. It’s a simple but powerful way ensure a blade component has all the scripts needed to render the view in one file. Do yourself a favour and get a Laracasts account so you don’t have to read this blog!
The following example uses Alpine.js to trigger an instance of a different javascript library and @once
to push scripts to the stack in your layout file.
In Laravel 9.x
you can condense the code to push the stack using:
@once
is used to only push the script to the stack a single time regardless of how many times this component is used.
If you never call <x-date-picker />
, you never pull in that script.
Alternatively, you can just manually import the script in your head tag but then it will be loaded on every request.