title: Laravel Collection Pagination
body: <p>I'm trying to paginate a collection, however I'm struggling to figure out if there's an easier way to do this.</p>
  
  <p>I have added the following macro in my <code>AppServiceProvider</code>:</p>
  
  <pre><code>Collection::macro('paginate', function($perPage) {
      $page = request()-&gt;page ?: 0;
  
      $slice = $this-&gt;chunk($perPage)[$page &gt; 0 ? $page - 1 : 0];
  
      return new LengthAwarePaginator($slice, $this-&gt;count(), $perPage, $page, [
          'path' =&gt; url()-&gt;current(),
      ]);
  });
  </code></pre>
  
  <p>This seems to be working for now, but I'm wondering if this will do in the long run? Is it possible to paginate the results before they become a collection? For example, here's how I get the collection I'm trying to paginate:</p>
  
  <p>In my repository I have:</p>
  
  <pre><code>private function eagerLoadQuotes()
  {
      return Quote::with([
          'user', 
          'home', 
          'home.customer', 
          'installation', 
          'installation.commission', 
          'installation.preInstallationSurvey', 
          'installation.jobSheet'
      ]);
  }
  
  public function unstarted()
  {
      return $this-&gt;eagerLoadQuotes()-&gt;find(
              Installation::doesntHave('preInstallationSurvey')
              -&gt;pluck('quote_id')
          );
  }
  
  private function mine($collection)
  {
      return $collection-&gt;filter(function ($item, $key) {
          return $item-&gt;user_id == auth()-&gt;user()-&gt;id;
      });
  }
  
  public function myUnstarted()
  {
      return $this-&gt;mine($this-&gt;unstarted());
  }
  </code></pre>
  
  <p>and then I get that in my controller:</p>
  
  <pre><code>public function myUnstarted()
  {
      $quotes = $this-&gt;installations-&gt;myUnstarted();
  
      return view('installations.viewAll', compact('quotes'));
  }
  </code></pre>
  
  <p>There are a number of other methods too which reuse the private methods.</p>