I have a question for my Symfony project.
I have this code which shows the number of rows in a given database table:
// BudgetRepository
public function countBudgets()
{
$qb = $this->createQueryBuilder('budget');
return $qb
->select('count(budget.id)')
->getQuery()
->getSingleScalarResult();
}
// BudgetController
public function all(EntityManagerInterface $entityManager)
{
$repository = $entityManager->getRepository(Budget::class);
$count = $repository->countBudgets();
return $this->render("budget/budget.html.twig", ['numberOfBudgets' => $count]);
}
It works, but it only works inside budget/budget.html.twig
by usung {{ numberOfBudgets }}
.
How can I make it work on every page (specifically I want it to be placed in my file called _sidebar.html.twig
? Right now if I place {{ numberOfBudgets }}
onto _sidebar.html.twig
I get this error message “Variable “numberOfBudgets” does not exist”.
How should I rewrite the code?