Answer the question
In order to leave comments, you need to log in
How to add handler to all Laravel pages?
Good afternoon, the project already has a lot of pages and controllers.
The task in the menu is to add the number of messages from the database. The menu is connected as a component to each page.
The question is, how can I transfer data from the database to all pages at once?
So far, I see only such a solution: on each page in each controller function in return, pass a variable with a quantity. But this is a very long way, maybe there is something more concise?
Answer the question
In order to leave comments, you need to log in
Laravel has a special method for sharing information in all views. Which is called: Sharing Data With All Views
Ie . write to AppServiceProvider
View::share([
'messageCount' => Message::count(),
]);
You can make a component and push it into the desired layout
https://laravel.com/docs/8.x/blade#components
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question