title: Laravel: Load more data from controller into view
body: <p>i would like to ask how can i display more data by using js or ajax and call Controller displaying the result into my view. Until now i 'm displaying 8 posts with limit(8) for the first time. Then i 'm using ajax to call the controller and bring each time more 8 posts. The problem i 'm facing is that the posts are the same each time i call the controller or the ajax. This is my </p>
  
  <blockquote>
    <p>PostsController</p>
  </blockquote>
  
  <pre><code>class PostsController extends Controller {
      public function index(Request $request)
      {
          $posts = Post::orderBy('created_at', 'desc')-&gt;limit(4)-&gt;get();
          $categories = Category::all();
  
         if ($request-&gt;ajax()) {
             $view = view('posts.load', compact('posts', 'categories'))-&gt;render();
             return response()-&gt;json(['html'=&gt;$view]);
         }
  
          return view('posts.display', compact('categories', 'posts'));
      }
  }
  </code></pre>
  
  <blockquote>
    <p>The controller is called by web, route</p>
  </blockquote>
  
  <p><code>Route::get('/posts', 'PostsController@index');</code></p>
  
  <blockquote>
    <p>I call the foreach method into my posts.display</p>
  </blockquote>
  
  <pre><code>    &lt;div class="col-md-8 col-md-offset-2" id="grid" style="padding-top: 40px;"&gt;
      @foreach($posts as $post)
          @if(strlen($post-&gt;body) &lt; "701")
              &lt;div class="item"&gt;
                  &lt;div class="col-col-12 tr-share share" style="padding: 1px 0 1px 0"&gt;
                      &lt;span class="time"&gt;{{$post-&gt;created_at}}&lt;/span&gt;  
                      &lt;a href=""&gt;
                          &lt;span class="emoji"&gt;
                              {{Emoji::findByName("link")}}
                          &lt;/span&gt;
                      &lt;/a&gt;
                      &lt;a href=""&gt;
                          &lt;span class="emoji"&gt;
                              {{Emoji::findByName("mail")}}
                          &lt;/span&gt;
                      &lt;/a&gt;
                      &lt;a href=""&gt;
                          &lt;span class="emoji"&gt;
                              {{Emoji::findByName("pin")}}
                          &lt;/span&gt;
                      &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="tr-text"&gt;
                      &lt;div class="col-md-12 td-text"&gt;
                          &lt;p&gt;&lt;a&gt;{{$post-&gt;body}}&lt;/a&gt;&lt;/p&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="tr-tag"&gt;
                      &lt;div style="padding: 1px 0 1px 0; text-align: center"&gt;
                          {{Emoji::findByName("tag")}}Tag: 
                              @foreach($categories as $category)
                                  @if($category-&gt;id == $post-&gt;category_un)
                                      &lt;a href=""&gt;&lt;span class="tag-un"&gt;#{{$category-&gt;name}}&lt;/span&gt;&lt;/a&gt;,
                                  @endif
                                  @if($category-&gt;id == $post-&gt;category_fac)
                                      &lt;a href=""&gt;&lt;span class="tag-fac"&gt;#{{$category-&gt;name}}&lt;/span&gt;&lt;/a&gt;
                                  @endif
                              @endforeach
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="tr-options"&gt;
                      &lt;a href="" style="color: #dca20f"&gt;&lt;span class="cool"&gt;(0) {{Emoji::findByName("cool")}}&lt;/span&gt;&lt;/a&gt;
                          &lt;hr class="vt-hr" /&gt;
                      &lt;a href="" style="color: #AA8062"&gt;&lt;span class="poo"&gt;(0) {{Emoji::findByName("poo")}}&lt;/span&gt;&lt;/a&gt;
                          &lt;hr class="vt-hr" /&gt;
                      &lt;a href="" style="color: #000000"&gt;&lt;span class="speech"&gt;(0) {{Emoji::findByName("speech")}}&lt;/span&gt;&lt;/a&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          @elseif(strlen($post-&gt;body) &gt; "700")
              &lt;div class="item"&gt;
                  &lt;div class="col-col-12 tr-share share" style="padding: 1px 0 1px 0"&gt;
                      &lt;span class="time"&gt;{{$post-&gt;created_at}}&lt;/span&gt;  
                      &lt;a href=""&gt;
                          &lt;span class="emoji"&gt;
                              {{Emoji::findByName("link")}}
                          &lt;/span&gt;
                      &lt;/a&gt;
                      &lt;a href=""&gt;
                          &lt;span class="emoji"&gt;
                              {{Emoji::findByName("mail")}}
                          &lt;/span&gt;
                      &lt;/a&gt;
                      &lt;a href=""&gt;
                          &lt;span class="emoji"&gt;
                              {{Emoji::findByName("pin")}}
                          &lt;/span&gt;
                      &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="tr-text"&gt;
                      &lt;div class="col-md-12 td-text"&gt;
                          &lt;p class="more"&gt;&lt;a&gt;{{$post-&gt;body}}&lt;/a&gt;&lt;/p&gt;
                          &lt;?php // echo strlen($post-&gt;body);?&gt;
                          &lt;!--- &lt;a&gt;&lt;span&gt;Διάβασέ την συνέχεια..&lt;/span&gt;&lt;/a&gt; --&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="tr-tag"&gt;
                      &lt;div style="padding: 1px 0 1px 0; text-align: center"&gt;
                          {{Emoji::findByName("tag")}}Tag: 
                              @foreach($categories as $category)
                                  @if($category-&gt;id == $post-&gt;category_un)
                                      &lt;a href=""&gt;&lt;span class="tag-un"&gt;#{{$category-&gt;name}}&lt;/span&gt;&lt;/a&gt;,
                                  @endif
                                  @if($category-&gt;id == $post-&gt;category_fac)
                                      &lt;a href=""&gt;&lt;span class="tag-fac"&gt;#{{$category-&gt;name}}&lt;/span&gt;&lt;/a&gt;
                                  @endif
                              @endforeach
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class="tr-options"&gt;
                      &lt;a href="" style="color: #dca20f"&gt;&lt;span class="cool"&gt;(0) {{Emoji::findByName("cool")}}&lt;/span&gt;&lt;/a&gt;
                          &lt;hr class="vt-hr" /&gt;
                      &lt;a href="" style="color: #AA8062"&gt;&lt;span class="poo"&gt;(0) {{Emoji::findByName("poo")}}&lt;/span&gt;&lt;/a&gt;
                          &lt;hr class="vt-hr" /&gt;
                      &lt;a href="" style="color: #000000"&gt;&lt;span class="speech"&gt;(0) {{Emoji::findByName("speech")}}&lt;/span&gt;&lt;/a&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          @endif
      @endforeach
  &lt;/div&gt;
  &lt;div class="ajax-load text-center" style="display:none"&gt;
      &lt;p&gt;&lt;img src=""&gt;&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <blockquote>
    <p>In the end i 'm using ajax, into the footer, that calls the controller
    via url and get the data with the same limitation</p>
  </blockquote>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(window).scroll(function() {
      var flag = 0;
      if($(window).scrollTop() + $(window).height() &gt;= $(document).height()) {
          $.ajax(
          {
              url: '/posts',
              type: 'get',
              /*beforeSend: function()
              {
                  $('.ajax-load').show();
              }*/
              data: {
                  'offset': flag,
                  'limit': 8
              },
              success: function(data){
                  $('.ajax-load').hide();
                  $("#grid").append(data.html);
                  flag += 8;
              }
          })
      }
  });
  
  function loadMoreData(){
    $.ajax(
          {
              url: '/posts',
              type: 'get',
              /*beforeSend: function()
              {
                  $('.ajax-load').show();
              }*/
              data: {
                  'offset': 0,
                  'limit': 8
              }
          })
          .done(function(data)
          {
              if(data.html == " "){
                  $('.ajax-load').html("No more records found");
                  return;
              }
  
              $('.ajax-load').hide();
              $("#grid").append(data.html);
              flag += 8;
          })
          .fail(function(jqXHR, ajaxOptions, thrownError)
          {
                alert('server not responding...');
          });
  }
  </code></pre>
  
  <p></p>
  
  <blockquote>
    <p>All the data are displayed by the ajax into a copy of posts.display code at posts.load</p>
  </blockquote>
  
  <pre><code> @foreach($posts as $post)
      @if(strlen($post-&gt;body) &lt; "701")
          &lt;div class="item"&gt;
              &lt;div class="col-col-12 tr-share share" style="padding: 1px 0 1px 0"&gt;
                  &lt;span class="time"&gt;{{$post-&gt;created_at}}&lt;/span&gt;  
                  &lt;a href=""&gt;
                      &lt;span class="emoji"&gt;
                          {{Emoji::findByName("link")}}
                      &lt;/span&gt;
                  &lt;/a&gt;
                  &lt;a href=""&gt;
                      &lt;span class="emoji"&gt;
                          {{Emoji::findByName("mail")}}
                      &lt;/span&gt;
                  &lt;/a&gt;
                  &lt;a href=""&gt;
                      &lt;span class="emoji"&gt;
                          {{Emoji::findByName("pin")}}
                      &lt;/span&gt;
                  &lt;/a&gt;
              &lt;/div&gt;
              &lt;div class="tr-text"&gt;
                  &lt;div class="col-md-12 td-text"&gt;
                      &lt;p&gt;&lt;a&gt;{{$post-&gt;body}}&lt;/a&gt;&lt;/p&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="tr-tag"&gt;
                  &lt;div style="padding: 1px 0 1px 0; text-align: center"&gt;
                      {{Emoji::findByName("tag")}}Tag: 
                          @foreach($categories as $category)
                              @if($category-&gt;id == $post-&gt;category_un)
                                  &lt;a href=""&gt;&lt;span class="tag-un"&gt;#{{$category-&gt;name}}&lt;/span&gt;&lt;/a&gt;,
                              @endif
                              @if($category-&gt;id == $post-&gt;category_fac)
                                  &lt;a href=""&gt;&lt;span class="tag-fac"&gt;#{{$category-&gt;name}}&lt;/span&gt;&lt;/a&gt;
                              @endif
                          @endforeach
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="tr-options"&gt;
                  &lt;a href="" style="color: #dca20f"&gt;&lt;span class="cool"&gt;(0) {{Emoji::findByName("cool")}}&lt;/span&gt;&lt;/a&gt;
                      &lt;hr class="vt-hr" /&gt;
                  &lt;a href="" style="color: #AA8062"&gt;&lt;span class="poo"&gt;(0) {{Emoji::findByName("poo")}}&lt;/span&gt;&lt;/a&gt;
                      &lt;hr class="vt-hr" /&gt;
                  &lt;a href="" style="color: #000000"&gt;&lt;span class="speech"&gt;(0) {{Emoji::findByName("speech")}}&lt;/span&gt;&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      @elseif(strlen($post-&gt;body) &gt; "700")
          &lt;div class="item"&gt;
              &lt;div class="col-col-12 tr-share share" style="padding: 1px 0 1px 0"&gt;
                  &lt;span class="time"&gt;{{$post-&gt;created_at}}&lt;/span&gt;  
                  &lt;a href=""&gt;
                      &lt;span class="emoji"&gt;
                          {{Emoji::findByName("link")}}
                      &lt;/span&gt;
                  &lt;/a&gt;
                  &lt;a href=""&gt;
                      &lt;span class="emoji"&gt;
                          {{Emoji::findByName("mail")}}
                      &lt;/span&gt;
                  &lt;/a&gt;
                  &lt;a href=""&gt;
                      &lt;span class="emoji"&gt;
                          {{Emoji::findByName("pin")}}
                      &lt;/span&gt;
                  &lt;/a&gt;
              &lt;/div&gt;
              &lt;div class="tr-text"&gt;
                  &lt;div class="col-md-12 td-text"&gt;
                      &lt;p class="more"&gt;&lt;a&gt;{{$post-&gt;body}}&lt;/a&gt;&lt;/p&gt;
                      &lt;?php // echo strlen($post-&gt;body);?&gt;
                      &lt;!--- &lt;a&gt;&lt;span&gt;Διάβασέ την συνέχεια..&lt;/span&gt;&lt;/a&gt; --&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="tr-tag"&gt;
                  &lt;div style="padding: 1px 0 1px 0; text-align: center"&gt;
                      {{Emoji::findByName("tag")}}Tag: 
                          @foreach($categories as $category)
                              @if($category-&gt;id == $post-&gt;category_un)
                                  &lt;a href=""&gt;&lt;span class="tag-un"&gt;#{{$category-&gt;name}}&lt;/span&gt;&lt;/a&gt;,
                              @endif
                              @if($category-&gt;id == $post-&gt;category_fac)
                                  &lt;a href=""&gt;&lt;span class="tag-fac"&gt;#{{$category-&gt;name}}&lt;/span&gt;&lt;/a&gt;
                              @endif
                          @endforeach
                  &lt;/div&gt;
              &lt;/div&gt;
              &lt;div class="tr-options"&gt;
                  &lt;a href="" style="color: #dca20f"&gt;&lt;span class="cool"&gt;(0) {{Emoji::findByName("cool")}}&lt;/span&gt;&lt;/a&gt;
                      &lt;hr class="vt-hr" /&gt;
                  &lt;a href="" style="color: #AA8062"&gt;&lt;span class="poo"&gt;(0) {{Emoji::findByName("poo")}}&lt;/span&gt;&lt;/a&gt;
                      &lt;hr class="vt-hr" /&gt;
                  &lt;a href="" style="color: #000000"&gt;&lt;span class="speech"&gt;(0) {{Emoji::findByName("speech")}}&lt;/span&gt;&lt;/a&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      @endif
  @endforeach
  </code></pre>
  
  <p>So my question is, why this brings always the same data? Every time i get at the end of the scroll and the ajax call the controller it sticks with the first 8 posts instead the 8 next one. Am i missing something or my code is wrong? Any help will be appreciate it.</p>