php - 在 Laravel 中使用 SQL 查询数组获取 html 图像源

标签 php html mysql laravel homestead

我正在做一个 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/

相关文章:

php - 将数组从 SQL 传递到 Swiftmailer

mysql - Angular 下载数据并将其存储在工厂中

MySQL 按特定起始字符排序

php - 如何查询 MySQL 数据库以确定用户是否连续登录 30 天还是 100 天?

php - 提交表单(jquery)并在 colorbox 中显示结果

php - 是否可以将Laravel 5.5.40降级到5.5.20?

php - Mysql 查找两张表

javascript - jQuery 检查输入字段内部是否有斜杠

javascript - 无法使用javascript使用公共(public)变量(i)更改每张幻灯片的字体样式

javascript - 使用 javascript 访问保存在一个页面上的用户输入值到另一页面上