Zaheer
4 months ago

Need help in saving Invoice data

Posted 4 months ago by Zaheer

I have a standard invoice with header info and multiple products in an html table. Can save header information but cannot figure out how to save the products. I am using a Modal form with Ajax to send data to Controller. My Ajax code is as under :

            $('#saveBtn').click(function (e) {
                e.preventDefault();
                $(this).html('Sending..');
                var tbldata = tbltoarr();
                $.ajax({
                    data: $('#purchaseForm').serialize(),
                    url: "{{ route('purchases.store') }}",
                    type: "POST",
                    dataType: 'json',
                    success: function (data) {
                        $('#purchaseForm').trigger("reset");
                        $('#code').focus();
                    },
                    error: function (data) {
                        var data2 = JSON.parse(data.responseText);
                        $('.alert-danger').html('');
                        jQuery.each(data2.errors, function(key, value){
                            jQuery('.alert-danger').show();
                            jQuery('.alert-danger').append('<li>'+value+'</li>');
                        });
                    }
                });
            });

and Controller code is :

 public function store(Request $request)
    {

$result = Purchase::updateOrCreate(['id' => $request->id],
    [   'date' => $request->date,
        'suppcode' => $request->code,
        'suppinv' => $request->invno,
        'suppdate' => $request->invdate,
    ]);

        return response()->json(['success'=>'Account saved successfully.']);
    }

tbldata is an array of items added to invoice, with following columns : Itemcode,Itemdesc,qty,rate,amount My question is how to attach tbldata to ajax post request and then how to handle/save it in controller.

Please sign in or create an account to participate in this conversation.