I am trying to update my form using AJAX POST method in Laravel, while submitting the form I am getting error:
Creating default object from empty value
I have tried this code to accomplish my goal.
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");
$('#result')[0].scrollIntoView(true);
$("#result").addClass("alert alert-success");
window.setTimeout(function(){
window.location.href = "/design";
}, 2000);
}else{
$('#error').html(results);
$('#error')[0].scrollIntoView(true);
$('#error').addClass("alert alert-danger");
}
}
});
});
and this is what I have done in Laravel Controller to accomplish my goal.
Here is my Laravel Controller page :
public function update(Request $request, $id)
{
// To Update
// $validator = Validator::make($request->all(), [
$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;
// Upload Image
$path=$request->file('design_image')->storeAs('public/images',$fileNameToStore);
}else{
$fileNameToStore='noimage.jpg';
}
$design=design::find($id);
$design->design_no=$request->input('design_no');
$design->desg_1=$request->input('desg_1');
$design->design_image=$fileNameToStore;
$design->desg_2=$request->input('desg_2');
$design->desg_3=$request->input('desg_3');
$design->desg_4=$request->input('desg_4');
$design->desg_5=$request->input('desg_5');
$design->desg_6=$request->input('desg_6');
$design->save();
return '1';
}