1

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);
                }
            }
        }); 
    }); 
5
  • What's inside $request->all()? Commented Oct 4, 2017 at 6:02
  • check my answer Commented Oct 4, 2017 at 6:12
  • @aldrin27 i coudn't understand Commented Oct 4, 2017 at 6:52
  • What i mean is what's the value of $request variable. Commented Oct 4, 2017 at 12:29
  • did you got the solution?? Commented Oct 4, 2017 at 13:17

1 Answer 1

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') 
     ]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

sorry for little late ,I used the alternative method which seems suitable for me , but i don't know whether it is the best practices or not if u could explain me
no prob! well its upto you if you are concerned about line of codes then 1st one is preferable if you are not then 2nd one is suitable for you apparently if you are using if else 2nd one is preferable!

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.