KHAN's avatar
Level 4

Laravel 5 Ajax Pagination

Can anyone give me an example of how i can get ajax pagination to work with Laravel 5. Ive seen some old tutorials using L4 but it does not seem to work with the new l5 pagination.

  if ($request->ajax()) {
            return response()->view('dashboard.partials.products')->with('products', $products))
                                                ->render();
        }
function getPosts(page) {
    $.ajax({
        url : '?page=' + page,
        dataType: 'json',
    }).done(function (data) {
        console.log(data);
        $('#products').html(data);
        location.hash = page;
    }).fail(function () {
        alert('Posts could not be loaded.');
    });
}

If fails the ajax request.

0 likes
6 replies
zachleigh's avatar

Are you sure you want to return the view and not just the data? Plus, youre calling render() on the response, not the paginator. I dont think thats going to work. Not sure though...

Are you actually making the pagination anywhere?

jlrdw's avatar

Have to calc for yourself since a custom paginator, search for custom paginator / pagination. https://laracasts.com/discuss/channels/general-discussion/custom-pagination-in-laravel5 If you study the following on the calcs to do you will get it: http://laravel.io/forum/11-13-2014-laravel-5-pagination EDIT: I saw @zachleigh answer, you need to worry about learning how to actually write a paginator prior to messing with ajax. A hint, they are one of the easiest things in the world to do.

KHAN's avatar
Level 4

@zachleigh, @jlrdw

Im calling render because i want to get the html. Ive already used paginate when i first load the page.

zachleigh's avatar

You need to paginate on every request, not only when you load the page.

Please or to participate in this conversation.