First, I logged in and it gives off an error "Undefined $scholar" in my view.
if (Auth::check()) {
$scholar = Scholar::where('user_id','=','Auth::User()->id')->count();
view()->share('scholar', $scholar);
}
second,I tried to dd(Auth::check()); and it returned false while im logged in.
Why is Auth::check() returns false even if the user is logged in?
if (Auth::check()) {
dd(Auth::check());
$scholar = Scholar::where('user_id','=','Auth::User()->id')->count();
view()->share('scholar', $scholar);
}
Three mistakes:
Your where clause is putting auth user id inside a quote, so it's evaluating it as a string hence the query is not finding any row
You aren't exactly fetching a Scholar row, instead you are querying the count?
You are accessing the authenticated user incorrectly
Try this:
P.S. Instead of
Auth::user()->idyou can also useAuth::id()- See more details here: https://laravel.com/docs/5.4/authentication