PHP - Assets Not Referencing To Public Folder (Laravel) - Stack Overflow
PHP - Assets Not Referencing To Public Folder (Laravel) - Stack Overflow
2024 Developer survey is here and we would like to hear from you! Take the 2024 Developer Survey
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
27
loaded on the page.
I'm using this code to load (it's only one example, there are more files):
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.
Thanks!
php laravel
Share Improve this question Follow asked Nov 18, 2016 at 17:22
Gabriel Augusto
467 1 9 23
What environment are you using to access your project? Local, hosted server, etc? – jackel414 Nov 18,
2016 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, 2016 at 20:13
what is your laravel version? – Mohammad Nurul Huda Rimon Nov 18, 2016 at 20:27
https://stackoverflow.com/questions/40682748/assets-not-referencing-to-public-folder-laravel?noredirect=1&lq=1 1/8
6/11/24, 6:32 PM php - Assets not referencing to public folder (Laravel) - Stack Overflow
Are you using Elixir for assets compilation? If so, what does your gulpfile look like? – JJWesterkamp Nov 19,
2016 at 0: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, 2016 at 2:13
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
29 to use public as well in the asset:
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.
Share Improve this answer Follow edited Jun 29, 2017 at 5:36 answered Jun 29, 2017 at 5:17
Bhojendra Rauniyar
84.8k 36 174 236
1 mix() instead of asset() if you use laravel mix – mercury Feb 26, 2022 at 19:34
25 ASSET_URL=public
https://stackoverflow.com/questions/40682748/assets-not-referencing-to-public-folder-laravel?noredirect=1&lq=1 2/8
6/11/24, 6:32 PM php - Assets not referencing to public folder (Laravel) - Stack Overflow
or
ASSET_URL=public/
or
ASSET_URL=/public
Share Improve this answer Follow edited Jun 10, 2021 at 9:11 answered Jul 27, 2020 at 13:11
Rakesh kumar Oad Sahilbalgotra100
1,338 1 15 24 410 5 7
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 <link href="{{ asset('css/style.css') }}" rel="stylesheet">
– Sahilbalgotra100 Feb 17, 2021 at 4: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, 2021 at 7: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, 2021 at 5:21
@Sahilbalgotra100 In which file to put <link href="{{ asset('css/style.css') }}" rel="stylesheet"> – Umair Jul
16, 2022 at 14:54
After you've savely and succesfully setup your .htaccess (like this), your method will work as you
would expect it to work:
7
<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 Google Chrome: Open Dev Tools > Open the Network tab > Check "Disable cache".
Share Improve this answer Follow edited Nov 5, 2019 at 8:30 answered May 24, 2019 at 14:17
Derk Jan Speelman
11.7k 4 31 46
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, 2021 at 13:25
6 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}"
Share Improve this answer Follow edited Jun 22, 2021 at 1:09 answered Jun 22, 2021 at 1:04
CodeToLife
4,010 2 44 31
2 Try this
Share Improve this answer Follow answered Dec 17, 2016 at 14:20
Dilajani Kotavadekar
381 1 2 9
https://stackoverflow.com/questions/40682748/assets-not-referencing-to-public-folder-laravel?noredirect=1&lq=1 4/8
6/11/24, 6:32 PM php - Assets not referencing to public folder (Laravel) - Stack Overflow
2 RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
Share Improve this answer Follow answered May 24, 2021 at 11:18
chacko
53 8
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,
1
<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
Share Improve this answer Follow answered Sep 14, 2021 at 7:42
Satya
496 5 7
In my case (using Laravel 10), I made these changes in the environment file (.env) of my laravel
project
1
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...
Share Improve this answer Follow edited Nov 10, 2023 at 3:59 answered May 27, 2023 at 18:37
Rishigesan
69 1 5
https://stackoverflow.com/questions/40682748/assets-not-referencing-to-public-folder-laravel?noredirect=1&lq=1 5/8
6/11/24, 6:32 PM php - Assets not referencing to public folder (Laravel) - Stack Overflow
The answer is already given, what is new in your answer – Nageen May 29, 2023 at 12:02
I got this issue in Laravel 10... Just thought of sharing it... What's wrong in it?? – Rishigesan May 30, 2023 at
5:23
sorry for my english, I have same problem in my case working with mike42/escpos-php was
because in xampp
0
I move to zlib.output_compression=on
zlib.output_compression=off
Share Improve this answer Follow answered Sep 20, 2021 at 4:55
andres felipe meza
mendez
19 1
stlye.css is loading and working for me from public folder. Don't forget to add rel="stylesheet"
Share Improve this answer Follow answered Apr 24, 2022 at 10:36
Muhammad Bilal
2,872 27 50
We may pass the public directly in the asset function. Here is an example
0 <!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')) }}">
https://stackoverflow.com/questions/40682748/assets-not-referencing-to-public-folder-laravel?noredirect=1&lq=1 6/8
6/11/24, 6:32 PM php - Assets not referencing to public folder (Laravel) - Stack Overflow
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
Share Improve this answer Follow answered May 27, 2023 at 4:14
Imtiaze
71 9
Answer is already been accepted, still like to say , check the file name if it's style.css or styles.css,
or something else
0
Share Improve this answer Follow answered 2 days ago
satish
111 11
https://stackoverflow.com/questions/40682748/assets-not-referencing-to-public-folder-laravel?noredirect=1&lq=1 7/8
6/11/24, 6:32 PM php - Assets not referencing to public folder (Laravel) - Stack Overflow
In Google Chrome:
Open Dev Tools > Open the Network tab > Check "Disable cache".
-2
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
Share Improve this answer Follow edited Dec 17, 2020 at 6:23 answered Dec 16, 2020 at 19:51
khaleesi Mutsa Winsome
983 14 25 Machingura
19
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, 2021 at 19:04
Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question.
The reputation requirement helps protect this question from spam and non-answer activity.
https://stackoverflow.com/questions/40682748/assets-not-referencing-to-public-folder-laravel?noredirect=1&lq=1 8/8