Retrieve multiple data using ajax data from controller

Posted 5 months ago by jhutto

I'm using ajax to post a payment into mysql database with laravel controller. This is working fine. Now I'm trying to make pass more that one variable back to ajax to use in the view. Can you send more than one data item back to the ajax code in the view.

This works just fine.

 $.ajax({
                        url: "/somaspayments/payment",
                        type: "POST",
                        data: $form_data,
                        cache: false,
                        success: function (data) {
                                //alert('Your data updated');
                                data = JSON.parse(data);
                                console.log(data);
});

Controller: This works too.. It post the payments to the database. and will return all the payments made and then I can process the json data and display all the payments including the new one in the view.

public function makepayment(Request $request)
    {
        

        $StudentPayment = SomasStudentPayment::create([
             'RegistrationID'=> $request->get('registrationid'),
             'PaymentTypeID' => $request->get('paymentTypeId'),
             'PaymentDate' => Carbon::now(),
             'CheckNum' => $request->get('checknum'),
             'PaymentAmount' => $request->get('paymentamount'),
           
         ]);
        

        $StudentPayments = SomasStudentPayment::where('RegistrationID','=', $request->get('registrationid'))
            ->join('somasstudentpaymenttype', 'somasstudentclasspayments.PaymentTypeID', '=', 'somasstudentpaymenttype.id')
            ->select('*', DB::raw('DATE_FORMAT(PaymentDate, "%m/%d/%Y") as PaymentDateF') , DB::raw('FORMAT(PaymentAmount, 2) as PaymentAmountF') )
            ->get();

             
        return json_encode($StudentPayments);

    }

What I would like to do is send additional data processed in the controller back the ajax, but separate the data out into separate variables ajax.

Adding these items to the controller. $PaymentsTotal = DB::table('somasstudentclasspayments')->where('RegistrationID', $request->get('registrationid'))->selectRaw('RegistrationID, sum(PaymentAmount) as TotalPayments')->groupBy('RegistrationID')->get(); $paymentTotal = (int)$PaymentsTotal[0]->TotalPayments;

    $TuitionTotal = $SOMASRegistration->ClassFee + $SOMASRegistration->HomeOfficeFee + $SOMASRegistration->MaterialsFee;
    
                
    $DueAmount = $TuitionTotal - $paymentTotal ;


return json_encode($StudentPayments, $PaymentsTotal, $TuitionTotal, $DueAmount );

Just like you would if you were just using returning the view with variables.

I can insert these variables into the array that's created but then I have to figure out how to separate all of it in javascript....

//$StudentPayments->put('PaymentTotal', $paymentTotal);

Is their a way to return multiple data variables back to the view where I can process the updates in the view with javascript? I hope I have given enough information.

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