I have a project where users are assgned to a client and I wannt to share that info across views.
In AppServiceProvider I added
use View;
use Auth;
and then amended boot to
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
This is because of the Auth
is not working in AppServiceProvider
So your If condition return false
if you share data with all the views then your code like this without check Auth. then It will work.
$company = 'Some value';
view::share('company',$company);
dd($company); // for print output.
Solution - For Alternate option you have to make Helper
class.
At the time the providers boot
is run, the Auth guard has not been booted, so Auth::check()
returns false, and Auth::user()
returns null.
You could do the View::share
in a middleware, or perhaps in the constructor of a controller (the base controller to share it across the whole application, or some particular controller if you need it in some subset of routes).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With