I have laravel breeze app that had users table with many to many relationship with unit. I set a unit session to store a unit the user have. and there is a posibility where unit got deleted and some user doesnt have any unit anymore, how do i check if user doesnt have a unit and redirect it back to login with message to contact di administrator,
// First, check if the user has a unit
if (Auth::user()->units->count() == 0) {
// If not, redirect them back to the login page with a message
return redirect('/login')->with('message', 'Uh oh! Looks like you don't have any units. Please contact your administrator for help!');
}
Don't worry, you don't have to be a rocket scientist to figure this out! Just a few lines of code and you'll be back on track.
@Snapey i know who the user is, and i actually know how to check how many unit they have, but i dont know how to intercept when user login, so when they dont have any unit it should redirect back to login with message to cantact admin