gpt4 book ai didi

php - 如何在 Laravel Blade 模板中显示上传的图像?

转载 作者:搜寻专家 更新时间:2023-10-31 20:35:16 25 4
gpt4 key购买 nike

我搜索了我的问题并找到了一些解决方案,但这些都没有解决我的问题。我正在使用 laravel 5.2,在这里我想上传一张图片并在 View 中显示它。我可以上传任何图片并且它有效好吧。但是有两件事是:

  1. 我看到数据库中的 profilePic 列是 null。但是图像是完美上传到我的public/img文件夹。
  2. 以及如何在 View 中显示它?

我的 ProfileController 的存储数据是这样的:

$file = array('profilePic' => Input::file('profilePic'));

$destinationPath = 'img/'; // upload path
$extension = Input::file('profilePic')->getClientOriginalExtension();
$fileName = rand(11111,99999).'.'.$extension; // renaming image
Input::file('profilePic')->move($destinationPath, $fileName);

Profile::create($profile);

我的个人资料 Blade 是这样的:

@foreach($profiles as $profile)
<tr>
<td> <img src='{{ asset($profile->profilePic) }}'> </td>
</tr>
@endforeach

我的代码哪里出了问题?请帮忙。我是 Laravel 的初学者。提前致谢。

最佳答案

您正在通过 {{ asset($profile->profilePic) }} 引用 Assets 文件夹

您可以先注释掉 foreach 循环并将 src='{{ asset($profile->profilePic) }}' 替换为 src=' img/FILENAME.EXTENSION' 您应该会在 View 的 img 文件夹中看到您上传的个人资料图片。

数据库中的 profilePic 列是空白的,因为您没有使用配置文件图片路径更新数据库。您为 Controller 发布的代码仅将上传的图像保存在目标 img 文件夹中,但没有发生数据库更新。

关于php - 如何在 Laravel Blade 模板中显示上传的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37237208/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com