错误:在Laravel中使用Ajax Post方法更新表单时 [英] Error:While updating form Using Ajax Post method in laravel

查看:57
本文介绍了错误:在Laravel中使用Ajax Post方法更新表单时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更新我的Laravel表单,同时更新Laravel的file字段以外的其他字段,我正在使用hasfile()对此进行检查.

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

这是我的控制器页面:

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

这是我的Ajax电话:

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

推荐答案

首先,您需要像这样验证文件

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

现在,如果变量$fileNameToStore具有noImage.jpg,则您需要对其进行检查,这意味着您不打算在此处更新图像,在这种情况下,您需要更新以保持上次上传的文件完整,否则将像这样更新图像文件.

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

替代方法

您还可以在此处做出两个更新语句,以保持其清洁

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

这篇关于错误:在Laravel中使用Ajax Post方法更新表单时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆