Prabodhana's avatar

Laravel pagination not working

I use laravel pagination to paginate data. its work but if I request it page to it response empty data array.

This is mt function

$cashbook = tblMainCashBook::whereBetween('date',[$request->input('from'),$request->input('to')])->paginate(50);
        $cashbook2 = tblMainCashBook::whereBetween('date',[$request->input('from'),$request->input('to')])->get();
        $cashbooktotDr = $cashbook2->sum('dr');
        $cashbooktotCr = $cashbook2->sum('cr');
        $cashbookBal = $cashbooktotDr - $cashbooktotCr;

        $getLastBalDate = tblBalCarFor::get()->last();
        if(isset($getLastBalDate)){
            $balanceCarTot = $cashbookBal + $getLastBalDate->balance;
            $balCar = $getLastBalDate->balance;;
        }else{
            $balanceCarTot = $cashbookBal;
            $balCar = 0;
        }

        return response()->json(['cashbook'=> $cashbook,'Dr'=>$cashbooktotDr,'Cr'=>$cashbooktotCr,'Bal'=>$cashbookBal,'BalanceCarried'=> $balCar , 'BalanceCarriedTot'=>$balanceCarTot], 200);

What is the problem? thank you..

0 likes
3 replies
jlrdw's avatar

For this:

$cashbook = tblMainCashBook::whereBetween('date',[$request->input('from'),$request->input('to')])->paginate(50);

Is correct data showing in network tab. And no idea how response is handled.

Snapey's avatar

The problem is that the paginator is an object, which you destroy when you convert it to json.

jlrdw's avatar

Also, @prabodhana have you read:

https://laravel.com/docs/8.x/pagination#converting-results-to-json

Things are handled a little different in a response like yours, you don't really show enough code for us to figure out what you are doing.

Also look at using skip and take methods if needed.

Edit: With ajax and json responses, use the network tab to assist you in troubleshooting your code. It will guide you as to what's happening.

Please or to participate in this conversation.