【问题标题】:How to show uploaded image in Laravel blade template?如何在 Laravel 刀片模板中显示上传的图像?
【发布时间】:2016-09-11 06:09:29
【问题描述】:

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

  1. 我在数据库中看到profilePic 列是null。但是图像是 完美上传到我的 public/img 文件夹中。
  2. 我怎样才能在视图中显示它?

我的 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);

我的个人资料刀片是这样的:

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

我的代码中的问题在哪里?请帮助。我是 Laravel 的初学者。 提前致谢。

【问题讨论】:

    标签: php image laravel laravel-5.2


    【解决方案1】:

    您正在通过{{ asset($profile-&gt;profilePic) }} 引用资产文件夹

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

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

    【讨论】:

      猜你喜欢
      • 2018-09-05
      • 2019-08-07
      • 2020-04-21
      • 2020-09-22
      • 1970-01-01
      • 2019-06-12
      • 2014-09-14
      • 2014-11-07
      • 1970-01-01
      相关资源
      最近更新 更多