title: Laravel pagination object in javascript
body: <p>I have an index page where I make the listing of some hospitals with a pagination.</p>
  
  <p>I also have a searchbar where I can write an hospital name and the listing should be updated "in real time" with an AJAX request.</p>
  
  <p>My problem is that I am not able to re-use the laravel pagination object by javascript to create the pagination. And I do not want to manage it by pure javascript/jQuery. At the moment, I'm creating a list of all objects returned. But without pagination (so the list can be huge)</p>
  
  <p>So in my controller I have this function</p>
  
  <pre><code>public function index()
  {
      $hospitals = Hospital::orderBy('name')-&gt;paginate(15);
      return view('admin.hospitals.show', compact('hospitals'));
  }
  </code></pre>
  
  <p>In my view (<code>/admin/hospitals/show.blade.php</code>), I'm using this to create a pagination (I want to re-use this on ajax response)</p>
  
  <pre><code>&lt;ul&gt;
  @foreach($tasks as $task)
      &lt;li&gt;{{ $task-&gt;name }}&lt;/li&gt;
  @endforeach
  &lt;/ul&gt;
  {{ $tasks-&gt;links() }}
  </code></pre>
  
  <p>Which give my this result</p>
  
  <p><a href="https://i.stack.imgur.com/87PZW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/87PZW.png" alt="enter image description here"></a></p>
  
  <p>When typing in the search bar, this ajax is called</p>
  
  <pre><code>$.ajax({
      type: "GET",
      url: '/admin/hospitals/search/'+ $('#hospital_search').val().trim(),
      success: function (data) 
      {
          if ( data.hospital.length == 0 ) 
          {
              $('#hospital_result').html('&lt;div class="alert alert-warning"&gt;No hospitals found&lt;/div&gt;');
          }
          else
          {
              //Actual code that give me the listing without pagination
              //Would be nice if I could do something like {{ data.hospital-&gt;links() }}
              var html = '&lt;ul&gt;';
              $.each( data.hospital , function( i , hospital ){    
                  html += '&lt;li&gt;&lt;a href="/admin/hospitals/'+ hospital.id +'"&gt;&lt;b&gt;' + hospital.name + '&lt;/b&gt;&lt;/a&gt;&lt;/li&gt;';});
              html += '&lt;/ul&gt;';
              $('#hospital_result').html(html);
          }
      },
      error: function (data) 
      {
          alert('Error:', data);
      }
  });
  </code></pre>
  
  <p>And my <code>search</code> function</p>
  
  <pre><code>public function search($term = null)
  {       
      if( !$term )
      {
          $hospital = Hospital::orderBy('name')-&gt;get();
      }
      else
      {
          //Get match on name
          $hospital = Hospital::where('name' , 'LIKE' , '%'.$term.'%')
                          -&gt;orderBy('name')
                          -&gt;get(); //Should be replaced by -&gt;paginate(15) when JS will be replaced
      }
  
      //Return as JSON
      return response()-&gt;json(['success' =&gt; true, 'hospital' =&gt; $hospital]);
  }
  </code></pre>
  
  <p>How to use the <code>-&gt;links()</code> on the <code>data</code> object in the ajax response?
  Or should I change my logic and load a specific view after ajax request?</p>