我搜索了我的问题,找到了一些解决方案,但这些都不能解决我的问题。我使用的是laravel 5.2,在这里我想上传一张图片并在视图中显示。我可以上传任何图片,效果很好。但有两件事:
- 我看到数据库中的
profilePic
列是null
。但是图像完美地上传到我的public/img文件夹中 - 我该如何在视图中显示它
我的配置文件控制器的存储数据如下:
$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
我的代码中的问题在哪里?请帮忙。我是拉拉威尔的初学者。提前感谢。
您正在通过{{ asset($profile->profilePic) }}
引用资产文件夹
您可以先注释掉foreach
循环,然后用src='img/FILENAME.EXTENSION'
替换src='{{ asset($profile->profilePic) }}'
。您应该在视图的img文件夹中看到您上传的个人资料图片。
数据库中的profilePic
列为空,因为您没有使用配置文件图片路径更新数据库。您为控制器发布的代码只将上传的图像保存在目标img文件夹中,但没有进行数据库更新。
在视图文件中,您可以使用src=";{{asset('storage/'.$post->image)}}",如果您已经使用php-artisan storage:link将存储文件夹从本地链接到公共。