D
D
driverx182017-08-26 19:09:11
Laravel
driverx18, 2017-08-26 19:09:11

How to implement Load More button in Laravel?

The layout has

load

(the toaster makes a link)
By clicking on this button, 2 publications should be loaded. I have never worked with this, I understand that I need to do something with Ajax, but I don’t understand what to do in PHP itself. How is this implemented?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maksim Markitantovm, 2017-08-27
@driverx18

In the controller

public function loadMore(Request $request)
    {
        $article= Article::orderBy('id', 'desc')->paginate(2);
        if ($request->ajax()) {
            return $article->toJson();
        }
       abort(404);
}

JS
$.ajax({
            type: 'GET',
            url: '/article?page=' + page,
            success: function (data) {
                let obj = JSON.parse(data);
                createArticleNode(obj);
                if (obj.to === obj.total) {
                    $('.button_more').hide();
                }
            }
        });

And you need to make a createArticleNode (obj) function that will create dom elements

O
Oleg, 2017-08-26
@Austin_Powers

If you want a ready-made solution or a manual, then google in the direction of "ajax pagination".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question