24

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?

Alberto
  • 1,348
  • 2
  • 14
  • 28

1 Answers1

34

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.

Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
  • 2
    do note that if `mix-manifest.json` is not present that `mix` will thrown an exception* – Quezler Jun 19 '18 at 11:14
  • So which one should I use to just include a `js` or `css` file? Or maybe doesn't matter? – Alberto Jun 19 '18 at 11:20
  • 4
    @Alberto If you're using versioning in your mix file, use `mix`. If you don't know what I'm talking about or aren't using versioning for your assets, use `asset` – Douwe de Haan Jun 19 '18 at 11:27
  • i have project in dev stage, and i still use localhost/project/public folder, asset works well, but mix tries to get assets from localhost/css/styles.css. How do I configure, that mix method will work same as asset? – Aleksandrs Nov 23 '20 at 09:14
  • @Aleksandrs do you mean your gulp file or the url that is generated? Because `mix` and `asset` should return the same url, but `mix` includes a version parameter – Douwe de Haan Nov 30 '20 at 12:19