D
D
DmitryLife2021-03-22 10:11:13
Laravel
DmitryLife, 2021-03-22 10:11:13

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

2 answer(s)
J
jazzus, 2021-03-22
@jazzus

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(),
]);

and use the $messageCount variable in all views

A
Anton Anton, 2021-03-22
@Fragster

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 question

Ask a Question

731 491 924 answers to any question