I have entity Travel, let's say it has 30 entries for example :
1-
2-
3-
4-
.
.
.
30-
I'd like to select 6 entity randomly, for example : 2, 14, 7, 25, 16, 1
I have tried this code but it works, but the results are always displayed by order ASC (3,4,5,6,7,8,).
public function getRandomTravelsFrontend()
{
$count = $this->createQueryBuilder('t')
->select('COUNT(t)')
->getQuery()
->getSingleScalarResult();
$qb = $this->createQueryBuilder('t')
->leftJoin('t.image', 'i')
->addSelect('i')
->Where('t.enabled = 1')
->setMaxResults(6)
->setFirstResult(rand(0, $count - 6));
return $qb->getQuery()->getResult();
}
How to display result by random order ? and is it possible to select 6 entities like this : 2, 14, 7, 25, 16, 1 ?