title: Recursive ->toArray() for Laravel
body: <p>I'm using Laravel as an API an AngularJS front end.  Part of this is when the app loads I preload the app with the user signed in to save an http request.</p>
  
  <p>Is there an recursive <code>-&gt;toArray()</code> that will loop through all properties and convert all eloquent models to jsonable arrays?</p>
  
  <p>I have made one manually in L3 but have not found something similar in the docs.</p>
  
  <p>PortalController.php
  <code>$user-&gt;medications</code> is an array of eloquent objects.</p>
  
  <p><strong>Does not work</strong></p>
  
  <pre><code>&lt;?php
  
  class PortalController extends BaseController {
  
      public function getIndex()
      {
          $user = User::find(Auth::user()-&gt;id);
          $user-&gt;healthProfile = $user-&gt;getHealthProfile();
          $user-&gt;medications = $user-&gt;getItems('medication');
  
          return View::make('portal.index', [
              'init' =&gt; [
                               // The array of medications disappear.
                  'user' =&gt; $user-&gt;toArray()
              ]
          ]);
      }
  
  }
  </code></pre>
  
  <p><strong>Works</strong></p>
  
  <pre><code>&lt;?php
  
  class PortalController extends BaseController {
  
      public function getIndex()
      {
          $user = User::find(Auth::user()-&gt;id);
          $user-&gt;healthProfile = $user-&gt;getHealthProfile();
          $user-&gt;medications = $user-&gt;getItems('medication')-&gt;toArray(); //boop
  
          return View::make('portal.index', [
              'init' =&gt; [
                              // The array of medications still exists.
                  'user' =&gt; $user-&gt;toArray()
              ]
          ]);
      }
  
  }
  </code></pre>
  
  <p>views/portal/index.blade.php</p>
  
  <pre><code>&lt;script&gt;
      (function() {
  
          'use strict';
  
          angular.module('portal.init', [])
  
              .service('Init', [function() {
                  return function() {
                      return {{ json_encode($init) }};
                  }
              }]);
  
      })();
  &lt;/script&gt;
  </code></pre>
  
  <p>My L3 class that converts recursively, hoping there is a native solution.</p>
  
  <pre><code>    public static function to_array($models)
      {
          if ($models instanceof Laravel\Database\Eloquent\Model) {
              $models = $models-&gt;to_array();
          }
  
          if (is_array($models)) {
              foreach ($models as $key =&gt; $value) {
                  $models[$key] = self::to_array($value);
              }
          }
  
          return $models;
      }
  </code></pre>
  
  <h1>Update</h1>
  
  <p>// models/User.php</p>
  
  <pre><code>    // ..etc.
  
  public function allergies()
  {
      return $this-&gt;hasMany('PortalItem', 'portal_user_id')
          -&gt;whereKey('allergy');
  }
  
  public function medications()
  {
      return $this-&gt;hasMany('PortalItem', 'portal_user_id')
          -&gt;whereKey('medication');
  }
  
  public function symptoms()
  {
      return $this-&gt;hasMany('PortalItem', 'portal_user_id')
          -&gt;whereKey('symptom');
  }
  
      // ..etc.
  </code></pre>
  
  <p>// controllers/PortalController.php</p>
  
  <pre><code>&lt;?php
  
  class PortalController extends BaseController {
  
      public function getIndex()
      {
          $user = User::with('healthProfile', 'medications', 'allergies', 'symptoms', 'procedures', 'immunizations')
              -&gt;whereId(Auth::user()-&gt;id)
              -&gt;first();
  
          return View::make('portal.index', [
              'init' =&gt; [
                  'user' =&gt; $user-&gt;toArray()
              ]
          ]);
      }
  
  }
  </code></pre>