title: Laravel: How to get duplicated records and group them together?
body: <p>The code below is what I have to get all the duplicated products (by title) and group them together. It works perfectly fine. However, I so many records in my Products table and getting all of them causes a performance issue. Is there a way this could be optimised to avoid getting all records and group them in one query? Thank you.</p>
  
  <pre><code>$products = Product::all();
  
  $groupsOfProducts = $products-&gt;groupBy('title');
  
  $duplicatedProductsGrouped = [];
  
  foreach($groupsOfProducts as $productGroup) {
  
      $productIsDuplicated = $productGroup-&gt;count() &gt; 1; 
  
      if($productIsDuplicated) {
  
          $duplicatedProductsGrouped[] = $productGroup;
  
      }
  
  }
  
  var_dump($duplicatedProductsGrouped);
  
  </code></pre>