title: Search Result pagination too much slow with length aware paginator in laravel
body: <p>I am trying to implement most relevant search in my application. </p>
  
  <p>I have a search page to search customers with search input field. The input field is matched with some fields in the database. I have following db structure.</p>
  
  <p><strong>Customer</strong></p>
  
  <blockquote>
    <p>email, status</p>
  </blockquote>
  
  <p><strong>Field</strong></p>
  
  <blockquote>
    <p>name, slug, status</p>
  </blockquote>
  
  <p><strong>Field Value</strong> (<em>Field Table contains fields like name, address, phone number etc.</em> )</p>
  
  <blockquote>
    <p>customer_id, field_id, value</p>
  </blockquote>
  
  <p><strong>I just take the input value from search page and match the values in field values table for fields like :</strong>  </p>
  
  <blockquote>
    <p>full_name, designation, address etc.</p>
  </blockquote>
  
  <p><em>For every matching I have increased a weight parameter and later I will order it by the weight. Here is the code.</em> </p>
  
  <p><strong>Search Key array:</strong></p>
  
  <pre><code>$keyword = $request-&gt;get('keyword');
  
  /**
  * Split key into string array
  */
  $keys = explode(' ', $keyword);
  
  $searchItems = [];
  foreach ($keys as $key){
      $key = str_replace(' ','', $key);
      $key = preg_replace('/[^a-zA-Z0-9_]/', '', $key);
      if($key){
          $searchItems[] = $key;
      }
  }
  $keys = $searchItems;
  </code></pre>
  
  <p><strong>Search Logic</strong></p>
  
  <pre><code>/**
   * Filter customer Ids based on search keys and field ids
   *
   * @param $fieldIds
   * @param $customerIds
   * @param $keys
   * @return mixed
   */
  public function searchCustomersForYellowPage($fieldIds, $customerIds, $keys)
  {
      $result =  $this-&gt;model-&gt;whereIn('customer_id', $customerIds)
                          -&gt;whereIn('field_id', $fieldIds)
                          -&gt;where(function ($query) use ($keys){
                              foreach ($keys as $key){
                                  $query-&gt;orWhere('value', 'LIKE', '%'.$key.'%');
                              }
                          })
                          -&gt;groupBy('customer_id')
                          -&gt;pluck('customer_id');
  
      return $result-&gt;sortByDesc(function ($item) use($fieldIds, $keys){
                  $weight = 0;
                  foreach ($fieldIds as $fieldId){
                      foreach ($keys as $key){
                          $exists = $this-&gt;model-&gt;where('field_id', $fieldId)
                                              -&gt;where('customer_id', $item)
                                              -&gt;where('value', 'LIKE', '%'.$key.'%')
                                              -&gt;exists();
                          if($exists)
                              $weight++;
                      }
                  }
                  return $weight;
  
      })-&gt;toArray();
  }
  </code></pre>
  
  <p>In this function, <code>$keys</code> parameter is the same as explained above, <code>$customerIds</code> is ids of all customers and <code>$fieldIds</code> are id of the fields used for search.</p>
  
  <p>This function returns array of all customer ids after search in most relevent order. Later I have used this array to get all customers and paginated with custom length aware paginator.</p>
  
  <p>The problem I am facing is it takes long time and after pagination when I load each page it takes same long time. Is there any way to execute this code only once and paginating search results over multiple pages so that it takes long time for first case and quicker when loading second third .... pages.</p>