图像未显示在视图中。 Laravel

问题描述 投票:0回答:6

在我的 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 }}&nbsp;{{ $follower->last_name }} Profile" width="50" height="50">

但是当我加载页面时我得到这个:

enter image description here

当我用萤火虫检查图像时,我看到了这个:

enter image description here

这是正确的 src,图像确实存在于我的 public/images/users/3/profilePictures/ 目录中

有人知道为什么会这样吗?

laravel laravel-blade
6个回答
16
投票

这可能是因为您所在的路由不代表基本 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')
助手。


16
投票

你可以试试

php artisan storage:link

否则你可以用这个

问题的简单修复(链接损坏错误) 在

.env
文件中

APP_URL=http://localhost

这替换为

APP_URL=http://localhost:8000

然后问题将得到解决。


1
投票

删除已经生成的符号链接,然后运行这个命令

php artisan storage:link

0
投票

如果您使用的是 laravel,请考虑从 laravel 控制的文件夹中排除公共文件夹或您的公共文件存储在 .htacccess 中的任何文件夹。

在 .htaccess 中这样做

# Exclude directory from rewriting RewriteRule ^(public/) - [L]


0
投票

之前的存储位置(不工作):

<img src="{{asset('storage/app/public/media/productImages/'.$list->image)}}" alt="" srcset=""> 

工作存放地点:

<img src="{{asset('storage/media/productImages/'.$list->image)}}" alt="" srcset="">

否则尝试:

  1. 删除在公用文件夹中创建的所有链接/快捷方式文件夹
  2. 然后通过运行重新创建链接:

php 工匠存储:链接


0
投票

Goto public Folder

删除存储快捷方式 然后为 storage 文件夹创建另一个快捷方式 Create Shortcut Storage Folder 然后运行 php artisan storage:link

© www.soinside.com 2019 - 2024. All rights reserved.