我正在做一个 Laravel 项目。我将用户的个人资料图片保存在服务器上的文件夹中,并将该照片的路径保存在数据库表中。我使用 sql 查询形成包含所有信息的数组,然后能够访问 HTML 中的信息,如下所示: {{profile.FirstName}}
等等。我正在尝试显示用户的个人资料图片。
我尝试过<img src="profile_pictures/{{profile.username}}"
还有src={{profile.profilePic}}
其中 profilePic 单元格具有照片的路径,但两者都不起作用。我是否应该尝试将路径保存在带有引号的表中,然后尝试第二个选项?或者有合适的方法来做到这一点吗?
最佳答案
使用asset()
asset() Generate a URL for an asset using the current scheme of the request (HTTP or HTTPS). See More
考虑用户模型具有 profile_picture
属性
其中 $user->profile_picture
将为您提供存储在路径中的图像路径
例如:
echo $thatUser->profile_picture;
//Will show this path "profilepictures/thatUser/image.jpg
//Real path: project/public/profilepictures/thatUser/image.jpg
要在 View 中显示该图像,请使用 asset()
<img src={{ asset($thatUser->profile_picture) }}" alt="that user image"></img>
关于php - 在 Laravel 中使用 SQL 查询数组获取 html 图像源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37596652/