Sergey Sob
Sergey Sob

Reputation: 813

laravel4 pagination over ajax

i have a function in my controller that's supposed to return me a paginated set of stuff, i do it exactly same way as in all other controller(where it works perfectly) but i get back an empty object. here's function code:

public static function getQuotes(){
    $quotes = Quote::orderBy('created_at','desc')->paginate(15);
    $links = $quotes->links();
    return json_encode(array( 'quotes' => $quotes, 'links' => $links ));        
}

here's the ajax request:

$.ajax({
     type: "POST",
     url: url,
     data: {datagram: datagram},
     success: function(data){
       console.log(data);    //debug
       catEditor.quoteStuff(data);
       typeof data == 'string' ? tools.flash(data) : 0;
     },
     error: function(xhr, status, err) {
       console.log(err.error+' '+xhr.responseText+' '+status);
     }
});

Upvotes: 1

Views: 1787

Answers (5)

Andrew Sparrow
Andrew Sparrow

Reputation: 21

Just convert $links object to string manualy before json encoding:

return json_encode(array( 'quotes' => $quotes, 'links' => (string)$links )); 

Good luck :)

Upvotes: 0

jminkler
jminkler

Reputation: 748

Paginate returns a Paginator object, not a collection

Upvotes: 0

krissanawat
krissanawat

Reputation: 626

$quotes = Quote::orderBy('created_at','desc')->paginate(15);
$links = $quotes->links();
return json_encode(array( 'quotes' => $quotes->getCollection()->toArray(), 'links' =>$links )); 

try this get collection is push instance from object and then convert to array

Upvotes: 0

jody_lognoul
jody_lognoul

Reputation: 817

First, try to return a simple object like this in your getQuotes() method:

return Response::json(array('name' => 'Steve', 'state' => 'CA'));

If you can get it from your ajax method, you should have a problem with your orm request. Else the problem is in the front.

Bye

Upvotes: 0

jody_lognoul
jody_lognoul

Reputation: 817

Can you, for a first step, try with this method :

return Response::eloquent(Quote::orderBy('created_at','desc')->paginate(15));

Bye

Upvotes: 1

Related Questions