【发布时间】:2019-04-27 17:53:06
【问题描述】:
我想更新我的文件输入图像,但是一旦我更新了 1 个图像,其他图像就会被删除,这是为什么呢?我想让它们保持上传时的状态。请帮帮我,谢谢。
Here is an image of the problem
控制器
更新,公共函数,这里是我放代码逻辑的地方
public function update(Request $request, $id)
{
$this->validate($request, [
'inflightmagz_date' => 'required',
'infightmagazine_pdf.*' => 'image|nullable|max:1999'
]);
$inflightmags = [];
if ($request->has('infightmagazine_pdf'))
{
//Handle File Upload
foreach ($request->file('infightmagazine_pdf') 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/infightmagazine_pdfs',$fileNameToStore);
array_push($inflightmags, $fileNameToStore);
}
$fileNameToStore = serialize($inflightmags);
}
$inflightmagContent = InflightMagazine::find($id);
$inflightmagContent->inflightmagz_date = $request->input('inflightmagz_date');
foreach ($inflightmags as $key => $value) {
$implodedInflight = implode(' , ', $inflightmags);
if($request->hasFile('infightmagazine_pdf')){
$inflightmagContent->infightmagazine_pdf = $implodedInflight;
}
}
$inflightmagContent->save();
return redirect('/admin/airlineplus/inflightmagazines')->with('success', 'Content Updated');
}
查看、编辑.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() !!}
【问题讨论】:
-
b'因为你只上传了一张图片,另外两张图片是空的,所以它的更新为空,这就是为什么你的两张图片被更新为空。因此,您需要检查图像是否具有空值而不是没有更新,并保持其旧值不变。
-
我该怎么做?好逻辑先生。语法如何?先生,您可以编辑我的代码吗?我非常需要它来工作
-
检查
hasFile里面的for循环 -
你能编辑我的代码先生吗:(所以你可以让我投票并标记你的答案:(
标签: php arrays laravel laravel-5 eloquent