1

I made a blog using laravel and php. In my main page I show all my posts from my database at once. I want to load my posts one at a time kinda like facebook does. I am using jquery ajax methods but I don't have a clear picture of how should I code it.

This is how I am trying to do it in my controller

// retrieve all the post from the database
$posts = DB::table('posts')->get();

return response() //what should I add here
// what should I do next

on the html side

$.ajax('/', {dataType: html}).done();

I want response as html and I am going to prepend it to the body.

Deeven
  • 413
  • 1
  • 5
  • 14
  • I think you're looking for endless scrolling, though they usually fetch more posts in each request, not just one. It's basically an interactive pagination. – JimL Mar 21 '18 at 20:28
  • ^ A solid example of which can be found here: https://datatables.net/extensions/scroller/examples/initialisation/server-side_processing.html – ficuscr Mar 21 '18 at 20:33
  • 2
    There's no reason to downvote this, it's a valid question, let's not chase people away all the time. On topic: you currently select *everything*. What you want to do is select a few records at a time, say 5 at a time. To do that, you need to look into what `LIMIT` clause is in SQL (yes, it limits number of records). In your javascript, you'd select 5 records (when page loads). Then, if user scrolls or does an action, you load next 5. Now, what this "action" is - is it a scroll event, is it a button click - that's up to you to decide. – N.B. Mar 21 '18 at 20:33
  • N.B. is talking about "pagination" and really the only way to go about scrolling through "very large data". Knowing some of this terminology maybe you can better Google and learn more about the topic to return with a more specific question. – ficuscr Mar 21 '18 at 20:35
  • 1
    @Deeven you need to supply how many items you have already displayed to your controller via the AJAX request. That way you can get 15 new items but skip 30 of them. – Ozair Patel Mar 21 '18 at 20:35
  • ^ a SQL LIMIT with "[offset](https://stackoverflow.com/questions/10119291/which-rows-are-returned-when-using-limit-with-offset-in-mysql)". – ficuscr Mar 21 '18 at 20:36

0 Answers0