Use Regexp in magento collection filter

Use Regexp in magento collection filter

Use Regexp in magento collection to filter the data, so magento provide regexp, that we can use to filter magento collection

<?php 

delcare(type_stricts=1);

namespace MageNick\RegexpCustom\Model;

use Magento\Product\Model\Product\ResourceModel\CollectionFactory as ProductCollection;

class Custom
{
  public function __construct(
  private ProductCollection $productCollection
  ) {
  }
  
  public function getFilterProductCollection(): array
  {
     $value = '[^red]';
     return $this->productCollection->create()
             ->addFieldToFilter('name', ['regexp', $value]);
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *