Home > Enterprise >  laravel asset() doesnt find assets in public
laravel asset() doesnt find assets in public

Time:02-22

I already put the folder named assets in my public folder and I should be able to access the assets like described on this page: https://laravel.com/docs/9.x/helpers#method-asset

But for some reason It wont load and the browser console gives this error:

Loading failed for the “http://localhost:8000/assets/js/jquery-3.4.1.min.js” when this should be the exact path

In the blade file I wrote this:

<script src="{{asset ('assets/js/jquery-3.4.1.min.js') }}"></script>

I already tried with URL::asset and It doesnt work

CodePudding user response:

Based on your comments:

I already have the folder assets in my public/storage folder

@GiuseppeP. Is the public/storage folder a symbolic link?

yes I made the symbolic link 'links' => [ public_path('storage') => storage_path('app/public'), ],

Your file is in the storage path.

This is accessible by using:

storage_path()

The storage_path function returns the fully qualified path to your application's storage directory. You may also use the storage_path function to generate a fully qualified path to a given file within the storage directory:

storage_path('app/public/assets/js/jquery-3.4.1.min.js')

Addendum

Alternatively, you can use the asset() helper by prefixing the path with storage/.... I.e:

The Public Disk

The public disk included in your application's filesystems configuration file is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores its files in storage/app/public.

Once a file has been stored and the symbolic link has been created, you can create a URL to the files using the asset helper:

asset('storage/assets/js/jquery-3.4.1.min.js')

CodePudding user response:

method asset() meaning go inside public folder so if it doesn't work

<script src="{{asset ('assets/js/jquery-3.4.1.min.js') }}"></script>

you should have in public folder assets folder then js folder then the file jquery-3.4.1.min.js

hopefully that help you

CodePudding user response:

you must set ASSET_URL in your .env file.

then run

php artisan config:cache
  • Related