S
S
suhuxa12017-10-19 11:14:01
Laravel
suhuxa1, 2017-10-19 11:14:01

How to use session in laravel service provider?

There was a need to display a variable with the user's balance for the entire view. I started doing this through the service provider in the boot method. It turned out that the session does not work in the service provider. Rather, it does not display an array of information about the user, although if you display session ('user') in any other file, info is displayed. But in the service provider it shows null. How to deal with it?
UPD: I did not dare to solve the problem through the creation of middleware. Still mid for one, service provider for another. So this code helped me:

view()->composer('*', function ($view)
        {
            $balance = User::select('balance')->where('login', session('user.login'))->first();
            $view->with('balance', $balance );
        });

Where 'balance' is a variable already available in the view

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2017-10-19
@Sanasol

https://laravel.com/docs/5.5/views#sharing-data-wi...

P
pantagruel964, 2017-10-19
@pantagruel964

Make middleware and share it there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question