Laravel:更新表单保留当前图像而不输入新图像不起作用



我有一个带图像字段的表单,插入所有带图像的字段可以很好地进行

编辑表单时->为了更新新图像,我使用取消链接功能删除以前的图像并用新图像更新。问题是,如果我不上传新的图片并提交表单,它就不起作用。

但是如果没有上传新图像,我想保留当前图像。我尝试了很多方法,甚至不使用取消链接函数,但都无法找到任何解决方案。请帮帮我,我真的需要这个解决方案。提前感谢

这是我在控制器中的更新功能

public function Update(Request $request, $id){

$PreviousPic = $request->Prev_pic;
$data = array();
$data['student_name'] = $request->student_name;
$data['matric_no'] = $request->matric_no;
$data['programme_name'] = $request->programme_name;
$data['faculty_name'] = $request->faculty_name;
$data['admission_year'] = $request->admission_year;
$data['contact_no'] = $request->contact_no;
$image = $request->file('pro_pic');
if ($image){ 
unlink($PreviousPic);
$image_name = date('dmy_H_s_i');
$ext = strtolower($image->getClientOriginalExtension());
$imageFullName = $image_name.'.'.$ext;
$uploadPath = 'media/';
$imageURL = $uploadPath.$imageFullName;
$success = $image->move($uploadPath,$imageFullName);
$data['pro_pic'] = $imageURL;
$Stdata = DB::table('students')->where('id', $id)->update($data);
return redirect()->route('student.index')
->with('success','Updated! The Student Data Updated Successfully');
}
}

这是我的编辑表单图像字段

<div class="form-group">
<label class="col-md-4 control-label" >Image</label>
<div class="col-md-5 inputGroupContainer">  
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-picture"></i></span>
<div class="upload-btn-wrapper">
<button class="btn">Upload a New Image</button>
<input type="file" name="pro_pic" />
</div>   
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Current Image </label>
<div class="col-md-5 inputGroupContainer">
<img src="{{ URL::to($StudentData->pro_pic)}}" height="150px" width="190px">
<input type="hidden" name="Prev_pic" value="{{$StudentData->pro_pic}}">
</div>
</div>

使用这个我可以上传新的图片删除旧的图片。但是,如果上载新图像字段为空并提交表单,则表单不会提交。

所以,我想如果我上传新的图片,它现在工作,如果我不上传新的图像,当前的图像将保持&提交表格。

#我正在使用Laravel 7

我已经找到了预期的答案

public function Update(Request $request, $id){
$PreviousPic = $request->Prev_pic;
$data = array();
$data['student_name'] = $request->student_name;
$data['matric_no'] = $request->matric_no;
$data['programme_name'] = $request->programme_name;
$data['faculty_name'] = $request->faculty_name;
$data['admission_year'] = $request->admission_year;
$data['contact_no'] = $request->contact_no;
$image = $request->file('pro_pic');
if ($image != null){ 
unlink($PreviousPic);
$image_name = date('dmy_H_s_i');
$ext = strtolower($image->getClientOriginalExtension());
$imageFullName = $image_name.'.'.$ext;
$uploadPath = 'media/';
$imageURL = $uploadPath.$imageFullName;
$success = $image->move($uploadPath,$imageFullName);
$data['pro_pic'] = $imageURL;
$Stdata = DB::table('students')->where('id', $id)->update($data);
return redirect()->route('student.index')
->with('success','Updated! The Student Data Updated Successfully');
}else{
$Stdata = DB::table('students')->where('id', $id)->update($data);
return redirect()->route('student.index')
->with('success','Updated! The Student Data Updated Successfully');
}
}

只需使用if-else语句即可找到解决方案,而且效果很好。

您需要从数据库传递旧图像的确切路径
您可以尝试连接,通过为image_nameimage_path制作两个单独的列

这是我的编辑图像代码

<div id="image" class="row" style="display: none;">
<div class="col-md-12">
<label for="text">Project Image</label>
<div class="form-group">
<input type="file" class="form-control" name="project_image"
value="{{ old('project_image',$currentProject->project_image) }}">
@if ($errors->has('project_image'))
<div class="text-danger">{{ $errors->first('project_image') }}</div>
@endif
</div>
</div>
</div>     

这是查看以前和新图像的div

<div class="box box-primary">
<div class="box-body">
<div class="box-header text-center">    
<h3 class="box-title">Project Cover Image</h3>
</div>
<div class="row">
<div class="col-md-12 text-center">
<img class="img-responsive" src="{{  URL::asset($currentProject->project_cover_image_path.'/'.$currentProject->project_cover_image_name)}}" alt="Photo">
<br>
<a class="btn btn-primary" data-toggle="modal" data-target="#modal-project-cover">
Change Cover Image
</a>
</div>
</div>

</div>
</div>  

这是实际编辑图像的模式

{{-- Modal dialogue to edit Project Cover Image --}}
<div class="modal fade" id="modal-project-cover">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header text-center">
<form method="POST" action="{{url('updateprojectcover', $currentProject->project_id)}}" enctype="multipart/form-data">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
@if (session('error'))
<div class="alert alert-danger">{{ session('error') }}</div>
@endif
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;
</span>
</button>
<div class="row">
<div class="col-md-12">
<h4 for="text">Project Cover Image</h4>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group text-center">
<input type="file" class="form-control" name="project_cover_image">
@if ($errors->has('project_cover_image'))
<div class="text-danger">{{ $errors->first('project_cover_image') }}
</div>
@endif
</div>
</div>
</div>
<div class="modal-footer text-center">
<button type="submit" class="btn btn-primary text-center">Yes</button>
</div>
</form>
</div>
</div>
</div>
</div>

这是我编辑图像的控制器

try 
{
$cover_name = $request->file('project_cover_image');
if (isset($cover_name)) 
{
$project_cover_image_name = $cover_name->getClientOriginalName();
$project_cover_image_name = str_replace(" ", "_", time() . $project_cover_image_name);
$cover_name->move(ImageUrlController::$project_cover_image_path, $project_cover_image_name);
}
Project::where('project_id', $project_id)
->update([
'project_cover_image_path' => ImageUrlController::$project_cover_image_path,
'project_cover_image_name' => $project_cover_image_name,
'updated_at' => Carbon::now('PKT'),
]);
return redirect()->back()->withInput();    
} 
catch (Exception $exception) 
{
return back()->withError($exception->getMessage())->withInput();
}

最新更新