jquery - 当我在 laravel 中更新时,图像不会被删除
问题描述
当我想单击关闭按钮时,我想使用 jquery 删除图像在我单击编辑按钮以更新数据后图像消失,图像没有被删除,它再次显示。这是我可以使用的代码。请帮忙。提前致谢。
这是我的控制器代码。
public function update(UpdateLocationHint $request, $id)
{
$hint = LocationHint::findOrFail($id);
$hint->location_id = $request->input('location_id'); // MB check the location_id from model Location
$hint->title = $request->input('title');
$hint->hint_text = $request->input('hint_text');
$hint->hint_solution_text = $request->input('hint_solution_text');
if (is_null($hint->hint_text)) {
$hint->hint_text = '';
}
if (is_null($hint->hint_solution_text)) {
$hint->hint_solution_text = '';
}
$imageFile = $request->file('hint_image_file');
$videoFile = $request->file('hint_video_file');
if ($imageFile != null) {
$fileName = md5(uniqid()).'.'.$imageFile->getClientOriginalExtension();
$uploadDirectoryPath = public_path('storage/uploads/hints/images');
$imageFile->move($uploadDirectoryPath, $fileName);
$hint->hint_image_path = $fileName;
}
if ($videoFile != null) {
$fileName = md5(uniqid()).'.'.$videoFile->getClientOriginalExtension();
$uploadDirectoryPath = public_path('storage/uploads/hints/videos');
$videoFile->move($uploadDirectoryPath, $fileName);
$hint->hint_video_path = $fileName;
}
$res = $hint->save();
if ($res != false) {
flash()->success('Successfully updated!');
} else {
flash()->error('Something wrong with saving!');
}
return redirect()->route('hints.edit', ['id' => $hint->id]);
}
这是更新视图刀片。
@if($hint->imageExists())
<div class="form-group">
<div class="image_uploaded_block">
<img style="width: 200px" src="{{url('/storage/uploads/hints/images/'.$hint->hint_image_path)}}" alt="{{ $hint->hint_image_path }}"/>
<div class="delete_image">
<a href="" class="btn btn-danger delete_image_btn"><i class="fa fa-times"></i></a>
</div>
</div>
</div>
@endif
这是jquery代码。
$(document).ready(function() {
$('.delete_image_btn').on('click', function (e) {
e.preventDefault();
$('.image_uploaded_block').remove();
$('.image_attributes').attr('disabled', false).attr('readonly', false);
});
解决方案
尝试这个
if ($imageFile != null) {
$fileName = md5(uniqid()).'.'.$imageFile->getClientOriginalExtension();
$uploadDirectoryPath = public_path('storage/uploads/hints/images');
$imageFile->move($uploadDirectoryPath, $fileName);
$hint->hint_image_path = $fileName;
} else {
$hint->hint_image_path = "";
}
推荐阅读
- save - 使用 to_netcdf 保存 xarray 数据集的选项
- python - 提取空值的索引号
- mysql - Eloquent 选择日期等于或高于今天的日期
- c# - 在 Entity Framework Core 中创建 OnBeforeSave 事件的正确方法
- angular - 在 beforeEach() 中初始化后更改模拟输入对象的值
- go - Golang atomic.StorePointer(...) 行为怪异
- python-3.x - 为什么这个语音识别代码不起作用?
- android - 如何使用低于 23 的 Android API 的材料组件以编程方式更改按钮颜色?
- python - 熊猫,时间序列:运行时间戳后的(ids,value)计数
- php - PHP SQL 错误帮助和安全改进