Answer the question
In order to leave comments, you need to log in
What is the best approach to use to pass a variable to all blades in Laravel?
On the site, for example, there is a menu in which you need to display categories and a footer in which you need to display a list of cities. At the same time, you should not be tied to the name of the frontend template on the backend, since it should be possible to display this data in other parts of the site template without rewriting/adding the backend code later.
I know of two possible ways to implement the above in Laravel:
1.
2.View::share('key', 'value');
View::composer('*', function ($view) {
//
});
Answer the question
In order to leave comments, you need to log in
Don't like "global" variables, use option #2.
If you don't like requests in service providers, make them in some third-party classes, and call this class in the service provider.
Or let the service provider refer to some class and get all the necessary variables for compositing.
Option number 3: subscribe to compositing events on your own and build your own logic.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question