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

_chris's avatar

Looking for a better way of doing this

Hi all,

I've got a project which has users, teams and roles. Users have a system-wide role and a role within each team. I have a controller which returns a list of models from the DB, let's say 'clients'. I'd like to return different things based on the users role. At the moment I'm doing this with a big if statement in the same controller but I know there is a nicer way, I just don't know what it is. I know this is Laravel 101 but it's something I've never quite figured a good solution to. Any ideas?

if(Auth::user()->currentTeam){

  $team_role = Auth::user()->teamRole(Auth::user()->currentTeam);

  //if user is within a team and user is 'admin' role
  //then send them all clients in team
  if($team_role == 'admin'){

      //...

  //if user is within a team and user is 'user' role
  //then send them only the clients assigned to them
  }else if($team_role == 'user'){

      //...

  }

}else{

  //if user is not within team and has 'admin' system role
  //then send them all clients in system

  if(Auth::user()->role == 'admin'){
      //...
  }

}
0 likes
2 replies
_chris's avatar

Thanks, but not really. I'm looking for a specific way to structure my code instead of using if statements to render different views/perform different queries.

Please or to participate in this conversation.