I have two fields for searching, that are:
Min
which is the minimum priceMaximum
which is the maximum priceHere, I want to display all the products which are between min and maximum price.
Help me I'm new to Symfony.
Here is my website
How can I filter?
There are two options :
Simple.
<form>
<input name="min" value="{{ min }}">
<input name="max" value="{{ max }}">
<input type="submit" value="Search">
</form>
public function listPhones(Request $request, EntityManagerInterface $em)
{
// get submitted values
$min = $request->get('min');
$min = $request->get('min');
$phones = $em->getRepository(Phone::class)->search($min, $max);
return ['phones' => $phones, 'min' => $min, 'max' => $max]
}
class PhoneRepository extends EntityRepository
{
public function search($min = null, $max = null)
{
$qb = $this->createQueryBuilder('p');
if (!is_null($min)) {
$qb->andWhere('p.price >= :min')
->setParameter('min', $min);
}
if (!is_null($max)) {
$qb->andWhere('p.price <= :max')
->setParameter('max', $max);
}
return $qb->getQuery()->getResult();
}
}