diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php index 408f3aa162..eaa12bac78 100644 --- a/app/Http/Controllers/Api/AssetModelsController.php +++ b/app/Http/Controllers/Api/AssetModelsController.php @@ -2,10 +2,12 @@ namespace App\Http\Controllers\Api; use App\Models\AssetModel; +use App\Models\Asset; use App\Http\Controllers\Controller; use App\Helpers\Helper; use Illuminate\Http\Request; use App\Http\Transformers\AssetModelsTransformer; +use App\Http\Transformers\AssetsTransformer; /** @@ -81,10 +83,25 @@ class AssetModelsController extends Controller public function show($id) { $this->authorize('view', AssetModel::class); - $assetmodel = AssetModel::findOrFail($id); + $assetmodel = AssetModel::withCount('assets')->findOrFail($id); return (new AssetModelsTransformer)->transformAssetModel($assetmodel); } + /** + * Display the specified resource's assets + * + * @author [A. Gianotto] [] + * @since [v4.0] + * @param int $id + * @return \Illuminate\Http\Response + */ + public function assets($id) + { + $this->authorize('view', AssetModel::class); + $assets = Asset::where('model_id','=',$id)->get(); + return (new AssetsTransformer)->transformAssets($assets, $assets->count()); + } + /** * Update the specified resource in storage. diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php index e74ae9e10f..a9a0263586 100644 --- a/app/Http/Controllers/Api/LocationsController.php +++ b/app/Http/Controllers/Api/LocationsController.php @@ -85,7 +85,7 @@ class LocationsController extends Controller { $this->authorize('view', Location::class); $location = Location::findOrFail($id); - return $location; + return (new LocationsTransformer)->transformLocation($location); } diff --git a/app/Http/Transformers/AssetModelsTransformer.php b/app/Http/Transformers/AssetModelsTransformer.php index e23f5ecbc7..57c23a44d7 100644 --- a/app/Http/Transformers/AssetModelsTransformer.php +++ b/app/Http/Transformers/AssetModelsTransformer.php @@ -18,6 +18,7 @@ class AssetModelsTransformer public function transformAssetModel (AssetModel $assetmodel) { + $transformed = [ 'id' => $assetmodel->id, 'name' => e($assetmodel->name), @@ -29,12 +30,13 @@ class AssetModelsTransformer 'category' => ($assetmodel->category_id) ? $assetmodel->category : null, 'fieldset' => ($assetmodel->fieldset) ? $assetmodel->fieldset : null, 'eol' => ($assetmodel->eol > 0) ? $assetmodel->eol .' months': 'None', - 'notes' => e($assetmodel->notes), + 'notes' => e($assetmodel->notes) ]; return $transformed; } + public function transformAssetModelsDatatable($assetmodels) { return (new DatatablesTransformer)->transformDatatables($assetmodels); } diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index ab18455bb0..6c704087b7 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -33,14 +33,13 @@ class AssetsTransformer 'notes' => $asset->notes, 'expected_checkin' => $asset->expected_checkin, 'order_number' => $asset->order_number, - 'companyName' => $asset->companyName, + 'company' => ($asset->company) ? $asset->company : null, 'location' => ($asset->assetLoc) ? $asset->assetLoc : null, 'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null, 'assigned_to' => ($asset->assigneduser) ? (new UsersTransformer)->transformUser($asset->assigneduser) : null, 'created_at' => $asset->created_at, 'purchase_date' => $asset->purchase_date, 'purchase_cost' => $asset->purchase_cost, - 'company' => ($asset->company) ? $asset->company->name: '', ]; diff --git a/app/Http/Transformers/LocationsTransformer.php b/app/Http/Transformers/LocationsTransformer.php index 0b405d4f50..43e776e2f5 100644 --- a/app/Http/Transformers/LocationsTransformer.php +++ b/app/Http/Transformers/LocationsTransformer.php @@ -19,6 +19,12 @@ class LocationsTransformer public function transformLocation (Location $location = null) { if ($location) { + + $assets_arr = []; + foreach($location->assets() as $asset) { + $assets_arr = ['id' => $asset->id]; + } + $transformed = [ 'id' => e($location->id), 'name' => e($location->name), @@ -28,7 +34,12 @@ class LocationsTransformer 'assets_checkedout' => $location->assets()->count(), 'assets_default' => $location->assignedassets()->count(), 'country' => e($location->country), + 'assets' => $assets_arr, ]; + + + + return $transformed; } diff --git a/resources/views/hardware/index.blade.php b/resources/views/hardware/index.blade.php index c197e1d229..32690a7a3e 100755 --- a/resources/views/hardware/index.blade.php +++ b/resources/views/hardware/index.blade.php @@ -74,7 +74,7 @@ @endif {{ trans('general.id') }} - {{ trans('general.company') }} + {{ trans('general.company') }} {{ trans('admin/hardware/table.image') }} {{ trans('admin/hardware/form.name') }} {{ trans('admin/hardware/table.asset_tag') }} diff --git a/resources/views/models/view.blade.php b/resources/views/models/view.blade.php index e4561cbf70..727f8f6001 100755 --- a/resources/views/models/view.blade.php +++ b/resources/views/models/view.blade.php @@ -44,19 +44,19 @@ name="modelassets" id="table" class="snipe-table" - data-url="{{route('api.models.show', $model->id)}}" + data-url="{{route('api.models.assets', $model->id)}}" data-cookie="true" data-click-to-select="true" data-cookie-id-table="modeldetailsViewTable"> - {{ trans('admin/companies/table.title') }} + {{ trans('admin/companies/table.title') }} {{ trans('general.id') }} - {{ trans('general.name') }} - {{ trans('general.asset_tag') }} - {{ trans('admin/hardware/table.serial') }} - {{ trans('general.user') }} + {{ trans('general.name') }} + {{ trans('general.asset_tag') }} + {{ trans('admin/hardware/table.serial') }} + {{ trans('general.user') }} {{ trans('table.actions') }} diff --git a/routes/api.php b/routes/api.php index 0249662fe9..d822efc3f6 100644 --- a/routes/api.php +++ b/routes/api.php @@ -63,6 +63,13 @@ Route::group(['prefix' => 'v1','namespace' => 'Api'], function () { ); + Route::get('models/{id}/assets', + [ + 'as' => 'api.models.assets', + 'uses'=> 'AssetModelsController@assets' + ]); + + Route::resource('models', 'AssetModelsController', ['names' => [