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