Skip to content

How to use addFieldToFilter & addFieldToSelect in magento2

I need to known how to use addFieldToFilter & addFieldToSelect in frontend? And can I use like below

$searchQuery = $wholeData["searchQuery"] ?? "";
$searchQuery = trim($searchQuery);
$agentCollection = $this->_agents->create()->addNameToSelect();
             
if ($agentId > 0) {
    $agentCollection->addFieldToFilter("id", $agentId);
} else {
    $agentCollection->addFieldToSelect("email")
            ->addFieldToFilter(
                    [
                            ["field"=>"email", "like"=>"%".$searchQuery."%"],
                            ["field"=>"contactno", "like"=>"%".$searchQuery."%"]
                    ]
            );
}