0

I'm working with Ajax in Laravel, Whenever Ajax is called the input fields values(which are empty string) are converted into null value. But I want empty string in response. How can I get this? I know I can map these values and convert/set null values to empty string but it is not efficient.

My Ajax Code:

$('#settingsForm').on('submit',(e) => {
  e.preventDefault();
  $.LoadingOverlay("show");
  const id = $('#id').val();
  const data = new FormData($('#settingsForm')[0]);
  $.ajax({
    type:'POST',
    url:  "{{url('company/settings')}}"+'/'+id,
    processData: false,
    contentType: false, 
    data: data,
    beforeSend: ()=>{
      console.log(data);
    },
    success: (resp) => {
      if(resp.code == 200){
        toastr.success(resp.type,resp.msg);
      }else
        toastr.error(resp.type,resp.msg);
      $.LoadingOverlay("hide");
    }
  });
  $('#tableRow').show();
  $('#settingsDiv').hide();
})

1 Answer 1

1

you can use a getter for each of those value like this :

public function getNameAttribute($value){
    return $value ?? '';
}

Then using the toArray() or toJson() method your null values will be converted into an empty string

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

Comments

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.