问候开发人员, 我试图创建一个数据库来存储图像。它存储该图像的URL,并且插入工作正常。
现在,我想显示存储在数据库中的图像,但是它只显示图像图标,而不显示图像。图像存储在公用文件夹内的图像文件夹中。我真的需要帮个忙提前致谢。下面我附上我的代码:
表单控制器:
public function store(Request $request)
{
$this->validate($request, [
'filename' => 'required',
'filename.*' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
if($request->hasfile('filename'))
{
foreach($request->file('filename') as $image)
{
$name=$image->getClientOriginalName();
$image->move(public_path().'/images/', $name);
$data[] = $name;
}
}
$form= new Form();
$form->filename=json_encode($data);
$form->save();
return back()->with('success', 'Your images has been successfully');
}
public function show()
{
$users = DB::select('select * from forms');
return view('display',['users'=>$users]);
}
插入页面
<form method="post" action="{{url('form')}}" enctype="multipart/form-data">
{{csrf_field()}}
<div class="input-group control-group increment" >
<input type="file" name="filename[]" class="form-control">
<div class="input-group-btn">
<button class="btn btn-success" type="button"><i class="glyphicon glyphicon-plus"></i>Add</button>
</div>
</div>
<div class="clone hide">
<div class="control-group input-group" style="margin-top:10px">
<input type="file" name="filename[]" class="form-control">
<div class="input-group-btn">
<button class="btn btn-danger" type="button"><i class="glyphicon glyphicon-remove"></i> Remove</button>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary" style="margin-top:10px">Submit</button>
</form>
</div>
<script type="text/javascript">
$(document).ready(function() {
$(".btn-success").click(function(){
var html = $(".clone").html();
$(".increment").after(html);
});
$("body").on("click",".btn-danger",function(){
$(this).parents(".control-group").remove();
});
});
</script>
显示页面:
@foreach ($users as $user)
<img src= url({{url('images/$user->filename')}});}/>
@endforeach
答案 0 :(得分:1)
您正在将多个文件名作为json存储在表格表的文件名列中。在刀片模板中,您要在img src中插入json而不是单个文件名。由于文件名存储为json,因此您需要解码json并为每个文件名运行foreach。
@foreach ($users as $user)
@php $filenames = json_decode($user->filename); @endphp
@foreach ($filenames as $filename)
<img src="{{ url('images/' . $filename) }}"/>
@endforeach
@endforeach
答案 1 :(得分:0)
如果它的json像这样
@foreach ($users as $user)
@php $images = json_decode($user->filename,true); @endphp
@foreach ($images as $image)
<img src="{{ url('images/'.$image) }}"/>
@endforeach
@endforeach
答案 2 :(得分:0)
您正在将控制器的文件名编码为数组,因此在刀片服务器模板中,必须先对其进行解码,然后再通过foreach
循环遍历并显示在img
标签中。希望对您有帮助。