diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php index eaa12bac78..1eb206d219 100644 --- a/app/Http/Controllers/Api/AssetModelsController.php +++ b/app/Http/Controllers/Api/AssetModelsController.php @@ -29,21 +29,31 @@ class AssetModelsController extends Controller public function index(Request $request) { $this->authorize('view', AssetModel::class); - $allowed_columns = ['id','image','name','model_number','eol','notes','created_at']; + $allowed_columns = ['id','image','name','model_number','eol','notes','created_at','manufacturer']; - $assetmodels = AssetModel::select(['id','image','name','model_number','eol','notes','created_at','category_id','manufacturer_id','depreciation_id','fieldset_id']) + $assetmodels = AssetModel::select(['models.id','models.image','models.name','model_number','eol','models.notes','models.created_at','category_id','manufacturer_id','depreciation_id','fieldset_id']) ->with('category','depreciation', 'manufacturer','fieldset') ->withCount('assets'); if ($request->has('search')) { - $assetmodels = $assetmodels->TextSearch($request->input('search')); + $assetmodels->TextSearch($request->input('search')); } + $offset = $request->input('offset', 0); $limit = $request->input('limit', 50); $order = $request->input('order') === 'asc' ? 'asc' : 'desc'; - $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at'; - $assetmodels->orderBy($sort, $order); + $sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'models.created_at'; + + switch ($sort) { + case 'manufacturer': + $assetmodels->OrderManufacturer($order); + break; + default: + $assetmodels->orderBy($sort, $order); + break; + } + $total = $assetmodels->count(); $assetmodels = $assetmodels->skip($offset)->take($limit)->get(); diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php index 316da642ab..3523c8067a 100755 --- a/app/Models/AssetModel.php +++ b/app/Models/AssetModel.php @@ -168,4 +168,19 @@ class AssetModel extends SnipeModel }); } + + /** + * Query builder scope to order on manufacturer + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderManufacturer($query, $order) + { + return $query->leftJoin('manufacturers', 'models.manufacturer_id', '=', 'manufacturers.id')->orderBy('manufacturers.name', $order); + } + + } diff --git a/resources/views/models/index.blade.php b/resources/views/models/index.blade.php index ae6bdf69bd..290cefe739 100755 --- a/resources/views/models/index.blade.php +++ b/resources/views/models/index.blade.php @@ -8,13 +8,7 @@ {{-- Page title --}} @section('header_right') - @if(Input::get('status')=='Deleted') - {{ trans('admin/models/general.view_models') }} - @else - - {{ trans('general.create') }} - {{ trans('admin/models/general.view_deleted') }} - @endif + {{ trans('admin/models/general.view_deleted') }} @stop @@ -39,7 +33,7 @@