title: List users from database with CodeIgniter 4
body: <p>I'm new in CodeIgniter 4 and i need to show a list of users with user details from multiple tables:
  users, users_details, user_contact_details, company, company_positions, departments.
  In table users_details i have foreign keys for company, company positions and departments.</p>
  <p>I really need your support.</p>
  <p>Here is my code.</p>
  <p>Thank you!</p>
  <p>Model User_Model.php</p>
  <pre><code>namespace App\Models;
  use CodeIgniter\Model;
  
  class User_Model extends Model {
      
      
      public function get_user()
      {
          return $this-&gt;db-&gt;table('users')
          -&gt;join('users_details','users_details.user_id_fk = users.user_id')     
          -&gt;join('user_contact_details','user_contact_details.user_id_fk = users.user_id')      
          -&gt;get()-&gt;getResultArray();
      }
      
          public function get_user_company_details()
      {
          return $this-&gt;db-&gt;table('users_details')
          -&gt;join('company','company.company_id = users_details.company_id_fk')          
          -&gt;join('company_positions','company_positions.position_id = users_details.position_id_fk')
          -&gt;join('departments','departments.department_id = users_details.department_id_fk')        
          -&gt;get()-&gt;getResultArray();
      }
     
  }
  </code></pre>
  <p>Controller Users.php</p>
  <pre><code>namespace App\Controllers;
  use CodeIgniter\Controller;
  use App\Models\User_Model;
  
  class Users extends BaseController
  {     
      protected $User_Model;
          
          public function __construct()
          {
              $this-&gt;User_Model = new User_Model();
          }
          
          public function list_users()
          {
              $data=[
                  'user' =&gt;$this-&gt;User_Model-&gt;get_user(),
              ];
              
           foreach ($data['user'] as $type) {
              
              $type = [
                  'company' =&gt; $this-&gt;User_Model-&gt;get_user_company_details(),
                  ];
       }
              echo view('users/list_users', $data);
          }
  
  }
  </code></pre>
  <p>View list_users.php</p>
  <pre><code>&lt;table id=&quot;useful_links&quot; class=&quot;table table-striped projects&quot; role=&quot;grid&quot; aria-describedby=&quot;example2_info&quot;&gt;
     &lt;thead&gt;
        &lt;tr&gt;
           &lt;td&gt;Id&lt;/td&gt;
           &lt;td&gt;Name&lt;/td&gt;
           &lt;td&gt;Phone&lt;/td&gt;
           &lt;td&gt;E-mail&lt;/td&gt;
           &lt;td&gt;Vacation days&lt;/td&gt;   
           &lt;td&gt;Position&lt;/td&gt;  
           &lt;td&gt;Active&lt;/td&gt;
           &lt;td&gt;Deleted&lt;/td&gt;
           &lt;td&gt;Options&lt;/td&gt;
        &lt;/tr&gt;
     &lt;/thead&gt;
     &lt;?php
        $n=1;
        foreach ($user as $row)
        {?&gt;
     &lt;tr&gt;
        &lt;td&gt;&lt;?php echo $n;?&gt;
        &lt;td&gt;&lt;div class=&quot;user-block&quot;&gt;
                &lt;span class=&quot;username&quot; style=&quot;margin-top:5px;&quot;&gt;&lt;?php echo $row['surname'].&quot; &quot;.$row['firstname'];?&gt; &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $row['user_phone'];?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $row['user_email'];?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $row['yearly_vacation_days'];?&gt;&lt;/td&gt;
        &lt;td&gt;
            &lt;?php  foreach ($company as $row)
        {
                echo $row['position_name'];
        }?&gt;
        &lt;/td&gt;
        &lt;td&gt;&lt;?php echo $row['user_active'];?&gt;&lt;/td&gt;
        &lt;td&gt;&lt;?php echo $row['user_deleted'];?&gt;&lt;/td&gt;
        &lt;td&gt;
           &lt;a href=&quot;&lt;?php echo site_url('/useful_links/editUseful_link/'.$row['link_id']);?&gt;&quot; class=&quot;btn btn-primary btn-sm&quot; href=&quot;#&quot;&gt;&lt;i class=&quot;fas fa-folder&quot;&gt;&lt;/i&gt; View&lt;/a&gt;&amp;nbsp;
           &lt;a href=&quot;&lt;?php echo site_url('/useful_links/editUseful_link/'.$row['link_id']);?&gt;&quot; class=&quot;btn btn-info btn-sm&quot;&gt;&lt;i class=&quot;fas fa-pencil-alt&quot;&gt;&lt;/i&gt; Edit&lt;/a&gt;&amp;nbsp;
           &lt;a href=&quot;&lt;?php echo site_url('/useful_links/delete/'.$row['link_id']);?&gt;&quot; class=&quot;btn btn-danger btn-sm&quot;&gt;&lt;i class=&quot;fas fa-trash&quot;&gt;&lt;/i&gt; Delete&lt;/a&gt;
        &lt;/td&gt;
        &lt;?php $n++;   }
           ?&gt;
  &lt;/table&gt;
  </code></pre>