Harish Karthick
Harish Karthick

Reputation: 720

Error:While updating form Using Ajax Post method in laravel

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

Upvotes: 1

Views: 303

Answers (1)

Mr. Pyramid
Mr. Pyramid

Reputation: 3935

First of all you need to validate your file like this

$this->validate($request,[
        'design_no'=>'required',
        'design_image'=>'nullable|image|mimes:jpg,png,jpeg|max:1999'
    ]);

Then you need to fetch the record that you are updating if you already have those record then ignore this step.

$designData = design::where('design_no',$id)->first();

Now you need to put a check on you variable $fileNameToStore if it has noImage.jpg means you are not updating image here in that case you need to update keep your last uploaded file intact else update your image file like this.

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

Alternative Approach

You can also make two update statements here to keep it clean like this

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

Upvotes: 1

Related Questions