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.


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">


  $(document).ready(function () {
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    /*  When click add button */
    $('#create-new-post').click(function () {
        $('#softwareModal').html("Add New Software");
   /* 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");
    $('body').on('click', '.delete-user', function () 
        var softwareEnq_id = $(this).data("id");
        if(confirm("Are You sure want to delete !")) 
                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) {
     submitHandler: function(form) {
      var actionType = $('#btn-save').val();
            url: "{{ url('softwareEnq') }}",
            type: "POST",
            data: $('#userForm').serialize(),
            dataType: 'json',
            contentType: false,
            cache: false,
            processData: false,
          success: function( data ) 
                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);
              $('#btn-save').html('Save Changes');
            //   console.log('Info:', data);
                    function() {
                    }, 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.