title: Pass data from PHP to AngularJS efficiently
body: <p>Right now, we're in the process of moving our frontend to AngularJS. I'm fetching all users with a model <code>Users</code>. Right after doing that, I'm passing the data to AngularJS. This is the code:</p>
  
  <pre><code>$users = Users::find();
  
  $users_array = array();
  
  foreach ($users as $user) {
  
      if ($user-&gt;user) {
          $birthDate = explode("-", $user-&gt;user-&gt;getBirthdate());
  
          if (count($birthDate) &gt; 2) {
              $age = (date("md", date("U", mktime(0, 0, 0, $birthDate[0], $birthDate[1], $birthDate[2]))) &gt; date("md")
                      ? ((date("Y") - $birthDate[2]) - 1)
                      : (date("Y") - $birthDate[2]));
           } else {
               $age = "";
  
           }
  
           $array = array('id' =&gt; $user-&gt;user-&gt;getId(), 'firstname' =&gt; $user-&gt;user-&gt;getFirstname(), 'lastname' =&gt; $user-&gt;user-&gt;getLastname(), 'birthDate' =&gt; $age, 'gender' =&gt; $user-&gt;user-&gt;getGenderNeat());
           array_push($users_array, $array);
      }
  }
  
  return $users_array;
  </code></pre>
  
  <p>This isn't very efficient, because I'm looping over all users in the database. I do need to do something similar to this, because I can't query the models in AngularJS. Are there any better options? If so, how can I do it?</p>