Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get pagination links using ajax (laravel 4.2)

Good day,

How can I pass e.g: $transactions->links(); when doing an ajax call on laravel?

Heres my Controller on a normal request:

$transactions = DB::table('x_general_transactions')
    ->whereBetween('x_general_transactions.date_at', array($startDate,$endDate))
    ->paginate(30);

VIEW

{{ $transactions->(array('from' => $startDate, 'to'=>$endDate))->links() }}

and on ajax request

if (Request::ajax())
    {
        //$divs is my data container
        $res = [$divs,$transactions->links()];
        return Response::json($res);
    }

so when I tried to use .load on my ajax code

 $.ajax({
          url: href,
          type: 'get',
          dataType: 'json',
          success: function(response){
            $('.pagination').load(response[1]);
         });
 });

But nothing happens, I also tried

console.log(response[1]);

ajax response:

[[[{"gt_xid":1230,"gts_xid":1231,"xid":4728,"rnr":4,"code":"OR#","link_code":"CI#","link_rnr":6,"g_type":25,"account_name":"Cash on Hand","debit":50.5,"credit":0,"description":"","date_at":"2015-10-25 16:25:19"},{"gt_xid":1230,"gts_xid":1231,"xid":4729,"rnr":4,"code":"OR#","link_code":"CI#","link_rnr":6,"g_type":25,"account_name":"Accounts Receivable - Trade","debit":0,"credit":50.5,"description":"","date_at":"2015-10-25 16:25:19"}]],{}]

result:object {}` which is empty.

like image 237
melvnberd Avatar asked Nov 19 '25 18:11

melvnberd


1 Answers

I hope this helps

Controller Response(use render to render html and send response)

if (Request::ajax())
{
 $res = array(
  'data' => $divs,
  'links' => $transactions->links()->render()
 );
 return Response::json($res);
}

ajax code

 $.ajax({
          url: href,
          type: 'get',
          dataType: 'json',
          success: function(response){
             console.log(response.data);//get data
             console.log(response.links);//get links
            $('.pagination').load(response.data);
         });
 });
like image 171
Rameez Rami Avatar answered Nov 22 '25 09:11

Rameez Rami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!