Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pascal83's avatar

On all page, i have a topmenu that needs to query the database... where should i place my Eloquent code

Hello to all, I'm creating a new website and i have a question about my top menu.... In my old website, i included a php page topmenu.php in my php template...... and in this top menu.... if user is logged, i query the database to check for new messages... or if not logged, i show register and login button..

But now... in Laravel... i create a template (main.blade.php) and i include my top menu And i created a view for a page... let's take contact us page.... i created contactus.blade.php and in this contactus.blade.php i extends to my main template : @extends('main')

But where should i query my database to check for new messages and other things ?

In middleware ?

I suppose that i cant put it in a specific controller because in EACH controller i will need to paste my code to query the database....

When user will navigate on my site... he/she can click to go on orders page, members page, profiles page, contact us page... so i suppose my code to query the database to check for new messages or notifications should not be paste on each controller.....

So where should i place this code ?

Thanks a lot !

Pascal

0 likes
2 replies
pascal83's avatar

Hello Snapey,

That works perfectly !! Thanks a lot for your great help!

Have a great day !

Pascal

Please or to participate in this conversation.