1

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';
}  

1 Answer 1

1

The problem lies here in $design=design::find($id);

I think $design has null in words $design=design::find($id); is returning null here??

if so you need to put a check

$design=design::find($id);
    if($design){
            $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'); 
    }

If you want to update your record then I would suggest the simple update query approach for you like this way

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') 
]);

Hope this will fix your issue :)

Sign up to request clarification or add additional context in comments.

14 Comments

When i analysis network design no passing correctly like below Content-Disposition: form-data; name="design_no" 6
yes its not entering when deleted it and recode (In find any spl char )its entering and throwing error like this: Integrity constraint violation: 1062 Duplicate entry '5' for key 'designs_design_no_unique'
you are getting this error because you already have a record in your table where design_design_no_unique =5 I assume
check my updated answer use update query for updating records :)
Thank you so much @Mr.Pyramid
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.