I am trying to update my Laravel form While updating other fields except file field the Laravel I am checking this using hasfile() it's getting failed.
If I update the file upload the condition works perfectly the only problem occur while I am updating the other form field without updating the file
Here my Controller Page:
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';
}
Here is my Ajax call:
$('.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);
}
}
});
});
$request->all()?$requestvariable.