26

I have the public folder inside my laravel project and I have some js and css files inside it.

I'm using the asset function and even though it's referencing to the public folder, my files aren't loaded on the page.

I'm using this code to load (it's only one example, there are more files):

<link href="{{ asset('css/style.css') }}" rel="stylesheet">

And on the browser's console, I'm geting something like this:

Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:8000/css/style.css

Well, I tried to revert the last commit, but no success. Tried to change to URL::asset() function, nothing. Tried everything from the following link: http://laravel.io/forum/09-17-2014-problem-asset-not-point-to-public-folder?page=1 and success.

Please, a little help?

Thanks!

Gabriel Augusto
  • 457
  • 1
  • 8
  • 23
  • What environment are you using to access your project? Local, hosted server, etc? – jackel414 Nov 18 '16 at 18:43
  • Try to read logs of apache/nginx or laravel logs under `/storage/logs`. You may find there more information. – Victor Rudkov Nov 18 '16 at 20:13
  • what is your laravel version? – Mohammad Nurul Huda Rimon Nov 18 '16 at 20:27
  • Are you using Elixir for assets compilation? If so, what does your gulpfile look like? – JJWesterkamp Nov 19 '16 at 00:31
  • Sorry taking too long. It's in my localhost. I'm using XAMPP. So, what I've done so far: I used to have a virtualhost in my httpd.conf file, I removed it and didn't work. I also tried localhost:8000/public/ but didn't work. I created a new project from laravel and copied everything from the other project to this one, and it worked. LOL. But what the hack is wrong with the other project? I'm using version 5.0.16. Not using Elixir. Thanks for you answers! – Gabriel Augusto Nov 22 '16 at 02:13
  • Try **My Solution** It will work **everywhere**. check Link https://stackoverflow.com/questions/25222509/laravel-not-detecting-files-from-public-folder/69517681#69517681 – pankaj Oct 10 '21 at 18:01

12 Answers12

27

I was having same problem. This is due to moving of .htaccess file from public to root of the project in order to serve localhost/project rather than localhost/project/laravel. And needed to use public as well in the asset:

<link href="{{ asset('public/css/app.css') }}" rel="stylesheet">

Or, modify the asset function from /Illuminate/Foundation/helpers.php

if (! function_exists('asset')) {
    /**
     * Generate an asset path for the application.
     *
     * @param  string  $path
     * @param  bool    $secure
     * @return string
     */
    function asset($path, $secure = null)
    {
        return app('url')->asset("public/".$path, $secure);
    }
}

The preceding method is not good way. Anyway this would have been easier if there was config for setting asset path.

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
22

Add in your env:

ASSET_URL=public 

or

ASSET_URL=public/

or

ASSET_URL=/public

which one u need u can do it...

Rakesh kumar Oad
  • 1,332
  • 1
  • 15
  • 24
  • 2
    For me ASSET_URL=http://localhost/project_name/public worked. ASSET_URL=public OR other not generate full path,it start from public like public/assets/css/main.css. app url/base url not included. Why? – Satish Feb 12 '21 at 15:12
  • u don't need to add these in local Laravel project work fine without adding these it required in some shared hosting Use assets like this it work – Sahilbalgotra100 Feb 17 '21 at 04:56
  • 1
    without /public it's not working on local machine using XAMPP. I will try it on hosting server which one is working . – Satish Feb 17 '21 at 07:25
  • Simply try asset('css/style.css') this one if not works then try to add the upper solution given by me in .env file it works not add all try one by one – Sahilbalgotra100 Feb 18 '21 at 05:21
  • @Sahilbalgotra100 In which file to put – Umair Jul 16 '22 at 14:54
6

After you've savely and succesfully setup your .htaccess (like this), your method will work as you would expect it to work:

<link href="{{ asset('css/style.css') }}" rel="stylesheet">

Be aware that all client-side URL requests (requests to images, JavaScript/CSS files, JSON) will be affected by the Rewrite Rules after you've setup your .htaccess.

Also be aware that it might take a while after you see the changes. Cache might interfere with these changes. So, make sure you clear your Laravel cache:

In your Shell run the following commands individually:

php artisan cache:clear
php artisan route:cache
php artisan config:cache
php artisan view:clear

And make sure to disable caching temporarily in your browser settings:

In Google Chrome: Open Dev Tools > Open the Network tab > Check "Disable cache".

Other browsers: search online how to do that.

Derk Jan Speelman
  • 11,291
  • 4
  • 29
  • 45
  • After run these command i am getting these errors (2/2) QueryException SQLSTATE[42S02]: Base table or view not found: 1146 Table 'arcade_oms.oc_order' doesn't exist (SQL: SELECT (SELECT COUNT(order_id) FROM .oc_order WHERE date_added <= '2021-02-15' AND date_added >= '2021-02-15' AND order_status_id = '1') AS pending – Siraj Ali Feb 15 '21 at 13:25
5

In my case in .env I tried

ASSET_URL=public

with both side slash and alone, nothing helped along with all related SO answers, then opened my old project and saw in. This worked for me finally (next to clearing views&caches, of course)

ASSET_URL="${APP_URL}"

im talking about localhost side

CodeToLife
  • 3,672
  • 2
  • 41
  • 29
2
<link href="{{ asset('/css/style.css') }}" rel="stylesheet">

Try this

2

Add this is in your .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]

It Will solve your problem. SURE

chacko
  • 53
  • 8
  • not working : http://127.0.0.1:8000/invoice/2021-07-28/28-07-21 IFE000.pdf. here public is missing. when i add public then this works – pankaj Aug 03 '21 at 10:25
1

In my case, I made these changes in the environment file (.env) of my laravel project

APP_URL=https://sitename.com
ASSET_URL=https://sitename.com/public

Everything was working fine in localhost... But when transferring my laravel project to production, assets were not loading properly... Then this helped me to fix that issue...

Rishigesan
  • 41
  • 4
0

Since you have moved your .htaccess file to the root folder you have to change the assets url wherever you are using. The URL should be like below,

<link href="{{ asset('public/css/style.css') }}" rel="stylesheet">

Or, You can just set the ASSET_URL as 'public' in the .env file,

ASSET_URL=http://localhost/projectname/public
Satya
  • 416
  • 5
  • 7
0

sorry for my english, I have same problem in my case working with mike42/escpos-php was because in xampp

I move to zlib.output_compression=on

the correct way is

zlib.output_compression=off

and assets work again

0

stlye.css is loading and working for me from public folder. Don't forget to add rel="stylesheet"

<link href="{{ asset('/css/style.css') }}" rel="stylesheet">
Billu
  • 2,733
  • 26
  • 47
0

We may pass the public directly in the asset function. Here is an example

<!DOCTYPE html>
<html lang="en" class="h-full">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="shortcut icon" href="{{ asset(mix('img/log-viewer-32.png', 'vendor/log-viewer')) }}">

    <title>Log Viewer{{ config('app.name') ? ' - ' . config('app.name') : '' }}</title>

    <!-- Style sheets-->
    <link href="{{ asset(mix('app.css', 'vendor/log-viewer')) }}" rel="stylesheet" onerror="alert('app.css failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')">
</head>

<body class="h-full px-3 lg:px-5 bg-gray-100 dark:bg-gray-900">
<div id="log-viewer" class="flex h-full max-h-screen max-w-full">
    <router-view></router-view>
</div>

<!-- Global LogViewer Object -->
<script>
    window.LogViewer = @json($logViewerScriptVariables);

    // Add additional headers for LogViewer requests like so:
    // window.LogViewer.headers['Authorization'] = 'Bearer xxxxxxx';
</script>
<script src="{{ asset(mix('app.js', 'vendor/log-viewer')) }}" onerror="alert('app.js failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')"></script>
</body>
</html>

after linking up the app.css and app.js it's not correctly loading, so i have added the public in asset function, and it works perfectly

<link href="{{ asset('public' . mix('app.css', 'vendor/log-viewer')) }}" rel="stylesheet" onerror="alert('app.css failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')">

<script src="{{ asset('public' . mix('app.js', 'vendor/log-viewer')) }}" onerror="alert('app.js failed to load. Please refresh the page, re-publish Log Viewer assets, or fix routing for vendor assets.')"></script>
Imtiaze
  • 43
  • 9
-2

And make sure to disable caching temporarily in your browser settings:

In Google Chrome:

Open Dev Tools > Open the Network tab > Check "Disable cache".

Other browsers: search online how to do that.

This worked for me. Seems the previous load of CSS and JS is in cache and until it is released the new ones do not load

jeffrey
  • 965
  • 14
  • 25
  • First, thank you for your answer and while sometimes your answer is relevant, here it is not. As no one did, I'll add an explanation without downvoting. The question mentions `Failed to load resource: the server responded with a status of 404 (Not Found)` which means the browser could not find the file at the path. If that statement was not present in the question than it might mean that old(cached) version of file is being loaded. Also, cached version would create questions like why my changes are not being reflected instead of very specific `asset not referencing to public ...` title. – Rishiraj Purohit May 17 '21 at 19:04