Laravel - Ajax file upload form - call to member function extension null

Hi All

I’m having some trouble in Laravel with a file upload in Ajax. Currently i’m getting an error 500 with Integrity constraint violation: 1048 Column 'sw_type' cannot be null (SQL: insert into tuningsoftware...

Currently my ajax script is using the Route::resource and the ajax script updates the record or creates a new record depending on if it can pull the ID of the record and match the first line of the request. Ideally i would like to retain this functionality if i can.

Please can someone tell me where i’m going wrong.

Controller

public function store(Request $request)
        { 
            
            // request()->validate([
            //     'sw_link' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            // ]);
     
            if ($files = $request->file('sw_link')) 
            {
                $sw_link = $request->sw_link->store('public/images');
            }
                $softwareEnq = \App\TuningSoftware::updateOrCreate(
            
                
            ['id' => $request->software_id],
            [  
                'sw_link' => $request->sw_link, 
                'sw_type' => $request->sw_type, 
                'sw_manual' => $request->sw_manual, 
                'sw_os' => $request->sw_os, 
                'status' => $request->status
            ]
        );
            return Response::json($softwareEnq);
        }

Upload file snippet in view:


                        <div class="col-sm-12">
                            <label>Upload File</label>
                            <input type="file" name="sw_link" id="sw_link">
                        </div>


JS

<script>
  $(document).ready(function () {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    /*  When click add button */
    $('#create-new-post').click(function () {
        $('#btn-save').val("create-user");
        $('#userForm').trigger("reset");
        $('#softwareModal').html("Add New Software");
        $('#ajax-crud-modal').modal('show');
    });
 
   /* When click edit */
    $('body').on('click', '#edit-user', function () {
      var softwareEnq_id = $(this).data('id');
      $.get('softwareEnq/' + softwareEnq_id +'/edit', function (data) {
         $('#softwareModal').html("Edit Enquiry");
          $('#btn-save').val("edit-user");
          $('#ajax-crud-modal').modal('show');
          $('#software_id').val(data.id);
          $('#sw_type').val(data.sw_type);
          $('#sw_os').val(data.sw_os);
          $('#sw_link').val(data.sw_link);
          $('#sw_manual').val(data.sw_manual);
          $('#status').val(data.status);
      })
   });
   //delete 
    $('body').on('click', '.delete-user', function () 
    {
        var softwareEnq_id = $(this).data("id");
        if(confirm("Are You sure want to delete !")) 
        {$.ajax({
                type: "DELETE",
                url: "{{ url('softwareEnq')}}"+'/'+softwareEnq_id,
                success: function (data) 
                {
                    $("#softwareEnq_id" + softwareEnq_id).remove();
                },
                error: function (data) 
                {
                    console.log('Error:', data);
                }
            });
        }
    });   
  });
 
 if ($("#userForm").length > 0) {
      $("#userForm").validate({
 
     submitHandler: function(form) {
 
      var actionType = $('#btn-save').val();
      $('#btn-save').html('Sending..');
      
      $.ajax({
            url: "{{ url('softwareEnq') }}",
            type: "POST",
            data: $('#userForm').serialize(),
            dataType: 'json',
            contentType: false,
            cache: false,
            processData: false,
          success: function( data ) 
            {
                $('#send_form').html('Submit');
                $('#position-1-success-changelog').show(function() 
                {
                    
                var notice = new PNotify({
                    title: 'Success',
                    text: 'Software Status Updated.',
                    type: 'success',
                    icon: 'fa-check-circle',
                    addclass: 'stack-bottomleft'
                }); //end function
            }); //end response  

            // console.log('Success:', data);
 
              $('#updateTable').trigger("reset");
              $('#ajax-crud-modal').modal('hide');
              $('#btn-save').html('Save Changes');
            //   console.log('Info:', data);
                setTimeout( 
                    function() {
                        window.location.reload(true);
                    }, 800);
          },
          error: function (data) {
              console.log('Error:', data); // remove this for production
              $('#btn-save').html('Save Changes');
          }
      });
    }
  })
}

Thank you in advance.

Where does sw_type get set? You showed us the form html for sw_link.

Hey @droopsnoot, sw_type is further up in the HTML form along with sw_os etc. I just took a snippet to display that the name of the input was set correctly.

The data is then serialised and sent to the controller where it updates/creates a new record.

That’s fine, but the error message was for sw_type, but the snippet of code you posted was for sw_link. As it’s complaining that sw_type is null and cannot be, I wondered whether there might be a typo in that part.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.