Error:While updating form Using Ajax Post method in laravel

左心房为你撑大大i 提交于 2019-12-11 09:45:51

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/46557915/errorwhile-updating-form-using-ajax-post-method-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!