Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

azureus_an's avatar

Checking data relation before login

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,

0 likes
4 replies
LaryAI's avatar
Level 58
// 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's avatar

you have no idea who the user is until they login ?

azureus_an's avatar

@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

Please or to participate in this conversation.