图像未显示在视图中。Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28007448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:43:21  来源:igfitidea点击:

Image not displaying in view. Laravel

laravelblade

提问by xslibx

In my Laravel project I'm trying to load an image into my view using blade but it displays as a broken link. I inspect it with firebug and the src is pointing to the image but nothing is displaying.

在我的 Laravel 项目中,我尝试使用刀片将图像加载到我的视图中,但它显示为断开的链接。我用萤火虫检查它,src 指向图像但没有显示任何内容。

My image is located in my project's public/images/users/3/profilePictures/

我的图片位于我项目的 public/images/users/3/profilePictures/

Here is my <img>in the view.blade.php

这是我<img>在 view.blade.php 中的

<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">

However I get this when I load the page:

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

enter image description here

在此处输入图片说明

When I inspect the image with firebug I see this:

当我用 firebug 检查图像时,我看到:

enter image description here

在此处输入图片说明

That is the correct src and the image does exist in my public/images/users/3/profilePictures/ directory

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

Anyone know why this is happening?

有谁知道为什么会这样?

回答by Mohebifar

This may be caused you are in a route which does not represent the base URL. You should generate the URL for your assets relative to the public/folder. Use URL::asset('path/to/asset')to generate the URL.

这可能是因为您所在的路由不代表基本 URL。您应该为您的资产生成相对于public/文件夹的 URL 。使用URL::asset('path/to/asset')生成的URL。

{{ URL::asset("images/users/{$follower->id}/profilePictures/50thumb-{$follower->profilePicture->url}") }}

Or as @lukasgeiter mentioned you can simply use asset('path/to/asset')helper.

或者正如@lukasgeiter 提到的,您可以简单地使用asset('path/to/asset')helper。

回答by Thilina Dharmasena

You can try with the

你可以试试

php artisan storage:link

else you can use this

否则你可以使用这个

A simple fix for the issue (linked broken error) in .envfile

.env文件中问题(链接损坏错误)的简单修复

APP_URL=http://localhost

this replace with the

这替换为

APP_URL=http://localhost:8000

then the issue will be fixed.

那么问题将得到解决。