S
S
suhuxa12017-07-18 22:35:24
Laravel
suhuxa1, 2017-07-18 22:35:24

How to properly implement "show more" button in laravel?

Hey!
I am learning Laravel, making a training site, and then I decided to implement the function of displaying articles by pressing the "show more" button. The idea is not new, I think the principle will be clear to many. So, only the following procedure sits in my head: There is a controller that processes the main page with, say, 10 articles. (clicking on the "show more" button will load 10 more). In the controller, we output articles from the database, send information to the view and output it in a loop there. Now, when you click on "show more", we send an Ajax request, which is processed by another controller. There we check how many pages have already been loaded (infa comes from ajax) and take a step from the settings, in our case, 10 articles. Further, in the controller, we directly output them in a loop, pack them into json and send them to the sender. The idea has already been implemented, everything works like clockwork. But I feel this is not quite the right solution. For example, today I needed to change the appearance of the output of articles. I had to do this both in the view and in the controller that handles the ajax. So what is the easiest way to solve this problem?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Melikhov, 2017-07-18
@suhuxa1

You can return a view in the second controller instead of sending the article json, which contains only the output of the articles (partial). In the first view then also use this partial. And if you need to change something, you will change it in one place (DRY).

D
Dmitry, 2017-07-21
@Astatroth

You can use the usual pagination built into Laravel, and for example this whistle .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question