From 5278dac2b0de4948634b51ae57c776d2f7978408 Mon Sep 17 00:00:00 2001 From: snipe Date: Sat, 28 Oct 2017 03:50:02 -0700 Subject: [PATCH] Eager loading assignedTo - I have no idea why this works --- app/Http/Controllers/Api/AssetsController.php | 2 +- app/Http/Transformers/AssetsTransformer.php | 20 +++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 2e240fff44..822a0c58dc 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -85,7 +85,7 @@ class AssetsController extends Controller } $assets = Company::scopeCompanyables(Asset::select('assets.*'))->with( - 'location', 'assetstatus', 'assetlog', 'company', 'defaultLoc', + 'location', 'assetstatus', 'assetlog', 'company', 'defaultLoc','assignedTo', 'model.category', 'model.manufacturer', 'model.fieldset','supplier'); diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 50be677c1e..04e21df56a 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -130,19 +130,19 @@ class AssetsTransformer public function transformAssignedTo($asset) { if ($asset->checkedOutToUser()) { - return $asset->assignedTo ? [ - 'id' => (int) $asset->assignedTo->id, - 'username' => e($asset->assignedTo->username), - 'name' => e($asset->assignedTo->getFullNameAttribute()), - 'first_name'=> e($asset->assignedTo->first_name), - 'last_name'=> e($asset->assignedTo->last_name), - 'employee_number' => e($asset->assignedTo->employee_num), + return $asset->assigned ? [ + 'id' => (int) $asset->assigned->id, + 'username' => e($asset->assigned->username), + 'name' => e($asset->assigned->getFullNameAttribute()), + 'first_name'=> e($asset->assigned->first_name), + 'last_name'=> e($asset->assigned->last_name), + 'employee_number' => e($asset->assigned->employee_num), 'type' => 'user' ] : null; } - return $asset->assignedTo ? [ - 'id' => $asset->assignedTo->id, - 'name' => $asset->assignedTo->display_name, + return $asset->assigned ? [ + 'id' => $asset->assigned->id, + 'name' => $asset->assigned->display_name, 'type' => $asset->assignedType() ] : null; }