In this tutorial im going to share how to check auth login if user login then execute the under code other wise redirect to login page.
Error: –
Attempt to read property "id" on null
Solution :-
if (Auth::check()) {
// User is logged in
$users = User::where('id', '=', auth()->user()->id)->first();
// Rest of your code...
// Place the rest of your code here
// ...
// Return the view based on the user's role
if ($getting_roll_id == 1) {
return view('pages.viewprofile', compact('slug', 'users', 'org_slug', 'getting_roll_id', 'slug_id', 'profile_user_url', 'user_id', 'get_country_name', 'get_state_name', 'get_city_name', 'gets_image', 'mobile_id', 'pay_user'));
} elseif ($getting_roll_id == 2) {
return view('manager/viewprofile', compact('slug', 'users', 'org_slug', 'getting_roll_id', 'slug_id', 'profile_user_url', 'user_id', 'get_country_name', 'get_state_name', 'get_city_name', 'gets_image', 'mobile_id', 'pay_user'));
} else {
return view('user/viewprofile', compact('slug', 'org_slug', 'users', 'getting_roll_id', 'slug_id', 'profile_user_url', 'user_id', 'get_country_name', 'get_state_name', 'get_city_name', 'gets_image', 'mobile_id', 'pay_user'));
}
} else {
// User is not logged in, redirect to the login page
return redirect()->route('login');
}