Laravel 5.7 - CSS and JS not loading using URL::asset

Ankit picture Ankit · Dec 12, 2018 · Viewed 17.8k times · Source

I am new in laravel. I setup laravel 5.7 on my local system and put js and css into public folder but when hitting my site url on browser, site is not loading any css and js.

Site structure :

app
bootstrap
config
database
public
 -css
 -js
 -images
resources
 -views
    -includes
    -layouts
    -pages
routes
tests
vendor
index.php
server.php
.env

Note : I have cut .htaccess and index.php files and put these on root to run site without public in path.

Here is how I am calling url : CSS :

{{ URL::asset('css/style.css') }}

JS :

{{ URL::asset('js/query-1.11.1.min.js') }}

When seeing source code, the url looks like this :

http://localhost/mysite/css/style.css

So can anybody help me out from this issue ?

Thanks in advance.

Answer

Sagar Gautam picture Sagar Gautam · Dec 12, 2018

The asset() helper prepends the base URL to the path you supply.

The base URL is the location of index.php (in this case: http://localhost/mysite/).

If you don't want index.php inside /public you will need to use public/ inside your asset path, like this:

asset("public/css/style.css")

Same for the js files, I hope you understand.