I don't get to understand the difference between using asset
or mix
Laravel helper when you have to include some js
or css
file.
<script src="{{ mix('/js/app.js') }}"></script>
vs
<script src="{{ asset('/js/app.js') }}"></script>
Both generates a link to public
folder so is there any difference between them? Is it better to use one of them instead of the other? In which context is it better?
Laravel Mix is a tool for compiling and optimizing assets in a Laravel app. It's similar to a build tool like gulp, Grunt and such like. it's specific to Laravel but can also be used externally as an npm package. Laravel Mix covered 80% of Webpack's use case to make compiling assets easier.
Laravel Mix, a package developed by Laracasts creator Jeffrey Way, provides a fluent API for defining webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors. In other words, Mix makes it a cinch to compile and minify your application's CSS and JavaScript files.
The assets folder in the resources directory is for files that need compilation, like views or LESS files. Compiled assets, or plain assets like CSS and JS files should be in public/assets/ .
Laravel Mix has a few methods for processing Javascript: mix. scripts() , mix. babel() and mix.
asset
is just a helper to get the correct path to the file you are using as parameter, where mix
also includes a version number, to help prevent caching of assets.
Read this page to understand more about mix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With