我正在尝试更新我的Laravel表单在更新除file
字段之外的其他字段Laravel我正在使用hasfile()
进行检查,但它已失败。
如果我更新文件上传条件完美,唯一的问题是在我更新其他表单字段而不更新文件时出现
这是我的控制器页面:
public function update(Request $request, $id)
{
$this->validate($request,[
'design_no'=>'required',
'design_image'=>'image|nullable|max:1999'
]);
// Handle file Upload
if ($request->hasFile('design_image')) {
// Get filename with image
$filenameWithex=$request->file('design_image');
// Get just file name
$filename=$_FILES['design_image']['name'];
// $filename=pathinfo($filenameWithex,PATHINFO_FILENAME);
// Get just ex
// $extension=pathinfo($filenameWithex,PATHINFO_EXTENSION);
// File Name To Store
$fileNameToStore=$filename;
$path=$request->file('design_image')->storeAs('public/images',$fileNameToStore);
}else{
$fileNameToStore='noimage.jpg';
}
design::where('design_no',$id)->update([
'desg_1' => $request->input('desg_1'),
'design_image'=> $fileNameToStore,
'desg_2' => $request->input('desg_2'),
'desg_3' => $request->input('desg_3'),
'desg_4' => $request->input('desg_4'),
'desg_5' => $request->input('desg_5'),
'desg_6' => $request->input('desg_6')
]);
$design->save();
return '1';
}
这是我的Ajax调用:
$('.submit').click(function(){
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var form = $('form')[0];
var update = new FormData(form);
var id =$('.designNo').val();
$.ajax({
type:"POST",
url:"/design_update/"+id,
processData: false, // Important!
contentType: false,
cache: false,
data:update,
success:function(results){
if (results==1) {
$("#result").html("Upadated Successfully");
}else{
$('#error').html(results);
}
}
});
});
答案 0 :(得分:1)
首先,您需要像这样验证您的文件
$this->validate($request,[
'design_no'=>'required',
'design_image'=>'nullable|image|mimes:jpg,png,jpeg|max:1999'
]);
然后,如果您已经拥有这些记录,则需要获取要更新的记录,然后忽略此步骤。
$designData = design::where('design_no',$id)->first();
现在您需要检查变量$fileNameToStore
是否有noImage.jpg
表示您没有在此更新图片,在这种情况下您需要更新保持上次上传的文件完好无损更新您的图片像这样的文件。
design::where('design_no',$id)->update([
'desg_1' => $request->input('desg_1'),
'design_image'=> ($fileNameToStore != 'noimage.jpg') ? $fileNameToStore : $designData->design_image,
'desg_2' => $request->input('desg_2'),
'desg_3' => $request->input('desg_3'),
'desg_4' => $request->input('desg_4'),
'desg_5' => $request->input('desg_5'),
'desg_6' => $request->input('desg_6')
]);
替代方法
你也可以在这里制作两个更新语句以保持它像这样
if ($request->hasFile('design_image')) {
// Get filename with image
$filenameWithex=$request->file('design_image');
// Get just file name
$filename=$_FILES['design_image']['name'];
// $filename=pathinfo($filenameWithex,PATHINFO_FILENAME);
// Get just ex
// $extension=pathinfo($filenameWithex,PATHINFO_EXTENSION);
// File Name To Store
$fileNameToStore=$filename;
$path=$request->file('design_image')->storeAs('public/images',$fileNameToStore);
design::where('design_no',$id)->update([
'desg_1' => $request->input('desg_1'),
'design_image'=> $fileNameToStore,
'desg_2' => $request->input('desg_2'),
'desg_3' => $request->input('desg_3'),
'desg_4' => $request->input('desg_4'),
'desg_5' => $request->input('desg_5'),
'desg_6' => $request->input('desg_6')
]);
}else{ // ignore design_image when you don't need to update that
design::where('design_no',$id)->update([
'desg_1' => $request->input('desg_1'),
'desg_2' => $request->input('desg_2'),
'desg_3' => $request->input('desg_3'),
'desg_4' => $request->input('desg_4'),
'desg_5' => $request->input('desg_5'),
'desg_6' => $request->input('desg_6')
]);
}