在我的 Laravel 项目中,我尝试使用刀片将图像加载到我的视图中,但它显示为断开的链接。我用萤火虫检查它并且 src 指向图像但没有显示任何内容。
我的图片位于我项目的 public/images/users/3/profilePictures/
这是我在 view.blade.php 中的
<img>
<img class="com-profile-picture" src="images/users/{{ $follower->id }}/profilePictures/50thumb-{{ $follower->profilePicture->url }}" alt="{{ $follower->first_name }} {{ $follower->last_name }} Profile" width="50" height="50">
但是当我加载页面时我得到这个:
当我用萤火虫检查图像时,我看到了这个:
这是正确的 src,图像确实存在于我的 public/images/users/3/profilePictures/ 目录中
有人知道为什么会这样吗?
这可能是因为您所在的路由不代表基本 URL。您应该为您的资产生成相对于
public/
文件夹的 URL。使用 URL::asset('path/to/asset')
生成 URL。
{{ URL::asset("images/users/{$follower->id}/profilePictures/50thumb-{$follower->profilePicture->url}") }}
或者正如@lukasgeiter 提到的,您可以简单地使用
asset('path/to/asset')
助手。
你可以试试
php artisan storage:link
否则你可以用这个
问题的简单修复(链接损坏错误) 在
.env
文件中
APP_URL=http://localhost
这替换为
APP_URL=http://localhost:8000
然后问题将得到解决。
删除已经生成的符号链接,然后运行这个命令
php artisan storage:link
如果您使用的是 laravel,请考虑从 laravel 控制的文件夹中排除公共文件夹或您的公共文件存储在 .htacccess 中的任何文件夹。
在 .htaccess 中这样做
# Exclude directory from rewriting RewriteRule ^(public/) - [L]
之前的存储位置(不工作):
<img src="{{asset('storage/app/public/media/productImages/'.$list->image)}}" alt="" srcset="">
工作存放地点:
<img src="{{asset('storage/media/productImages/'.$list->image)}}" alt="" srcset="">
否则尝试:
php 工匠存储:链接