From 0ab37af7ab01a91e29c5afa4e6263cacb687ac3e Mon Sep 17 00:00:00 2001 From: Jason Spriggs Date: Tue, 16 May 2017 15:30:11 -0400 Subject: [PATCH] Add in location parent and child information into API requests (#3582) * Add location hierarchy to LocationsTransformer * Add in parent_id --- app/Http/Controllers/Api/LocationsController.php | 2 +- app/Http/Transformers/LocationsTransformer.php | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php index c714104783..ad3f139f4d 100644 --- a/app/Http/Controllers/Api/LocationsController.php +++ b/app/Http/Controllers/Api/LocationsController.php @@ -21,7 +21,7 @@ class LocationsController extends Controller { $this->authorize('view', Location::class); $allowed_columns = ['id','name','address','address2','city','state','country','zip','created_at', - 'updated_at']; + 'updated_at','parent_id']; $locations = Location::select([ 'locations.id', diff --git a/app/Http/Transformers/LocationsTransformer.php b/app/Http/Transformers/LocationsTransformer.php index 8b53bab620..adbf9b26ee 100644 --- a/app/Http/Transformers/LocationsTransformer.php +++ b/app/Http/Transformers/LocationsTransformer.php @@ -26,6 +26,11 @@ class LocationsTransformer foreach($location->assets() as $asset) { $assets_arr = ['id' => $asset->id]; } + + $children_arr = []; + foreach($location->childLocations() as $child) { + $children_arr = ['id' => $child->id]; + } $array = [ 'id' => e($location->id), @@ -39,6 +44,8 @@ class LocationsTransformer 'assets' => $assets_arr, 'created_at' => Helper::getFormattedDateObject($location->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($location->updated_at, 'datetime'), + 'parent_id' => e($location->parent_id), + 'children' => $children_arr, ]; $permissions_array['available_actions'] = [