Sharing in Laravel 5.4

I have a project where users are configured per client and I don't want to share this information between views.

In AppServiceProvider, I added

use View;
use Auth;

      

and then fixed loading in

if ( Auth::check() )
            {
                $cid = Auth::user()->client_id;
                $company = \App\Clients::first($cid);
                view::share('company',$company);
            }

      

but if i dd ($ company) i get

Undefined variable: company 

      

+3


source to share


2 answers


This Auth

does not work inAppServiceProvider


So your if condition returns false

if you exchange data with all views then your code is like this without Auth checking. then it will work.



$company = 'Some value';
view::share('company',$company);

dd($company);     // for print output.

      

Decision. Alternatively, you should make a class Helper

.

+8


source


boot

Auth protection was not loaded during the startup of the providers , so it Auth::check()

returns false and Auth::user()

returns null.



You can do it View::share

in middleware, or perhaps in a controller constructor (a base controller to split it across the entire application, or some specific controller if you need it in some subset of routes).

+1


source







All Articles