我想更新上传的图像,但是一旦更新1张图像,其他图像就被删除了,为什么?我想将它们保留为上传后的样子。帮帮我,谢谢。
Here is an image of the problem
控制器
更新,公共功能,这是我放置代码逻辑的地方
public function update(Request $request, $id)
{
$this->validate($request, [
'title' => 'required',
'description' => 'required',
'fleet_image.*' => 'image|nullable|max:1999'
]);
$fleet = [];
if ($request->has('fleet_image'))
{
//Handle File Upload
foreach ($request->file('fleet_image') as $key => $file)
{
// Get FileName
$filenameWithExt = $file->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
//Get just extension
$extension = $file->getClientOriginalExtension();
//Filename to Store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $file->storeAs('public/fleet_images',$fileNameToStore);
array_push($fleet, $fileNameToStore);
}
$fileNameToStore = serialize($fleet);
}
else
{
$fileNameToStore='noimage.jpg';
}
if (count($fleet)) {
foreach ($fleet as $key => $value) {
$fleetContent = Fleet::find($id);
$fleetContent->title = $request->title[$key];
$fleetContent->description = $request->description[$key];
$implodedFleet = implode(' , ', $fleet);
if($request->hasFile('fleet_image')){
$fleetContent->fleet_image = $implodedFleet;
}
$fleetContent->save();
return redirect('/admin/airlineplus/fleets')->with('success', 'Content Updated');
}
}
return redirect('/admin/airlineplus/promotions')->with('success', 'Content Updated');
}
查看,edit.blade.php
{!! Form::open(['action'=>['Admin\FleetsController@update',$fleet->id], 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td> {{Form::text('title[]', $fleet->title, ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}}<br>
{{Form::textarea('description[]', $fleet->description, ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br>
<div class="card card-body col-md-8">
@foreach(explode(' , ' ,$fleet->fleet_image) as $content)
<img src="{{ asset('storage/fleet_images/' . $content) }}" style="width:50px;height:50px;"><br/>
{{ Form::file('fleet_image[]',['id'=>'exampleFormControlFile1']) }}<br/>
@endforeach
</div>
</td>
</tr>
</table>
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
</div>
{!! Form::close() !!}
答案 0 :(得分:0)
在您的控制器上
$fleet = array();
$fleetContent = Fleet::find($id);
if ($request->has('fleet_image')) {
for($i=0;$i<3;$i++){
if(isset($request->file('fleet_image')[$i])){
$filenameWithExt = $request->file('fleet_image')[$i]->getClientOriginalName();
//Get just filename
$filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
//Get just extension
$extension = $request->file('fleet_image')[$i]->getClientOriginalExtension();
//Filename to Store
$fileNameToStore = $filename.'_'.time().'.'.$extension;
//Upload Image
$path = $request->file('fleet_image')[$i]->move('public/fleet_images',$fileNameToStore);
array_push($fleet, $fileNameToStore);
}else{
$fleetContentExplode = explode(',',$fleetContent->fleet_image);
array_push($fleet,$fleetContentExplode[$i]);
}
}
}
在您的查看文件上
{!! Form::open(['action'=>['Admin\FleetsController@update',$fleet->id], 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}
<div class="table-responsive">
<table class="table table-bordered" id="dynamic_field">
<tr>
<td> {{Form::text('title', $fleet->title, ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}}<br>
{{Form::textarea('description', $fleet->description, ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br>
<div class="card card-body col-md-8">
@foreach(explode(' , ' ,$fleet->fleet_image) as $content)
<img src="{{ asset('storage/fleet_images/' . $content) }}" style="width:50px;height:50px;"><br/>
{{ Form::file('fleet_image[]',['id'=>'exampleFormControlFile1']) }}<br/>
@endforeach
</div>
</td>
</tr>
</table>
{{Form::hidden('_method', 'PUT')}}
{{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
</div>
{!! Form::close() !!}
注意::我刚刚从您的视图输入文本中删除了数组