Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel asset vs mix

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?

like image 806
Alberto Avatar asked Jun 19 '18 11:06

Alberto


People also ask

What's the difference between laravel and laravel mix?

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.

What is mix in laravel?

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.

What is asset in laravel?

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/ .

Does laravel mix use Babel?

Laravel Mix has a few methods for processing Javascript: mix. scripts() , mix. babel() and mix.


1 Answers

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.

like image 52
Douwe de Haan Avatar answered Sep 22 '22 18:09

Douwe de Haan