图像未显示在视图中。 Laravel
Image not displaying in view. Laravel
在我的 Laravel 项目中,我尝试使用 blade 将图像加载到我的视图中,但它显示为损坏的 link。我用 firebug 检查它并且 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">
但是我在加载页面时得到了这个:
当我用 firebug 检查图像时,我看到了这个:
那是正确的 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 控制的文件夹中排除 public 文件夹或您的 public 文件存储在 .htacccess 中的任何文件夹。
在 .htaccess 中这样做
# Exclude directory from rewriting RewriteRule ^(public/) - [L]
在我的 Laravel 项目中,我尝试使用 blade 将图像加载到我的视图中,但它显示为损坏的 link。我用 firebug 检查它并且 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">
但是我在加载页面时得到了这个:
当我用 firebug 检查图像时,我看到了这个:
那是正确的 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 控制的文件夹中排除 public 文件夹或您的 public 文件存储在 .htacccess 中的任何文件夹。
在 .htaccess 中这样做
# Exclude directory from rewriting RewriteRule ^(public/) - [L]