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

thetanaz's avatar

Best way to send data to persistent components like layouts and navbars?

I'm making an e-commerce store type website with react and inertia, and what I'm struggling with is sending data to actual components, like a layout or a navbar that wrap around {children} when rendering pages. Right now the way I have it setup is that I've made an API endpoint in my laravel backend and then I'm fetching the necessairy data - in my case categories and subcategories names and ids, using axios and then I populate the navbar. Issue with this is that the navbar does not appear straight away since it takes time to fetch the categories from the API and also it just doesn't feel right to me. Is there a better way to simply send the data from the backend to the component itself, rather than the page that's being rendered? Because the other option that I know of is sending the data to each page and then prop-drilling it on each page individually to the wrapping layout.

0 likes
6 replies
thetanaz's avatar

@vincent15000 It was right in front of my eyes the whole time. I was just casually accessing the auth object in whichever page I desired without really thinking about what's happening behind the scenes. I think this is exactly what I'm looking for and considering the fact that I'm just sending a few lines of text I think there won't really be a performance penalty for doing it on every page, considering that 80% of the pages will need that info anyways. Thanks!

1 like
thetanaz's avatar

@vincent15000 I even cached the categories/subcategories given the fact that they won't be changing. I made a function to break the cache just in case I decide to add a new category, I can simply run that in artisan tinker (I think).

1 like
Ben Taylor's avatar

@thetanaz just run the break cache function in the relevant model lifecycle events like created, updated, deleted etc.

2 likes

Please or to participate in this conversation.