How to get users by group? #638
              
                
                  
                  
                    Answered
                  
                  by
                    datamweb
                  
              
          
                  
                    
                      code353535
                    
                  
                
                  asked this question in
                Q&A
              
            -
| Sorry, I'm still very new. Below is my code where I want to list users but this is not happening  public function index()
    {
        
        $user = auth()->user();
        if (! $user->inGroup('superadmin', 'admin')) {
            return redirect()->to(site_url('/'))->with('error', 'Hata! <br> Bu sayfayı görme yetkiniz yok.');;
        }
        
        $this->userModel = new usermodel();
        $data['user'] = $this->userModel->select('users.*')->orderBy('id' , 'DESC')->findAll();
        
            return view('Admin/Yonetici/kullanici',$data);
    }all i can get (username <?php if($user): ?>
<?php $i = 1; ?>
         <?php foreach($user as $user): ?>
                                                
	<tr>
	<td><?= $i++ ?></td>
	<td><?php echo $user = $user->username ?></td> | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            datamweb
          
      
      
        Feb 15, 2023 
      
    
    Replies: 1 comment 2 replies
-
| Hello, welcome to Shield. What I understand is that you want to list users who are in group  public function getUsersByGroup($groupName = 'users', $returnFields= 'username'){
  $getUsersByGroup = $this->db->table('auth_groups_users')
    ->select($returnFields)
    ->join('users', 'users.id = auth_groups_users.user_id')
    ->getWhere(['group' => $groupName])
    ->getResult();
   
return($getUsersByGroup);
}Then use it as follows: $userModel = model(UserModel::class);
dd($userModel->getUsersByGroup($groupName = 'superadmin', $returnFields= ['username', 'active', 'fname', 'lname'])); | 
Beta Was this translation helpful? Give feedback.
                  
                    2 replies
                  
                
            
      Answer selected by
        code353535
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
Hello, welcome to Shield.
This issue is not directly related to the Shield. You must master the CI4 framework.
What I understand is that you want to list users who are in group
superadmin.For this, you can add the following function to your custom
UserModel:Then use it as follows: