diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 17ff6c0371..307efb1867 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -371,6 +371,9 @@ class AssetsController extends Controller case 'assigned_to': $assets->OrderAssigned($order); break; + case 'created_by': + $assets->OrderByCreatedByName($order); + break; default: $assets->orderBy($column_sort, $order); break; diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index 17693fccf4..d7ee423249 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -80,6 +80,10 @@ class AssetsTransformer 'assigned_to' => $this->transformAssignedTo($asset), 'warranty_months' => ($asset->warranty_months > 0) ? e($asset->warranty_months.' '.trans('admin/hardware/form.months')) : null, 'warranty_expires' => ($asset->warranty_months > 0) ? Helper::getFormattedDateObject($asset->warranty_expires, 'date') : null, + 'created_by' => ($asset->adminuser) ? [ + 'id' => (int) $asset->adminuser->id, + 'name'=> e($asset->adminuser->present()->fullName()), + ] : null, 'created_at' => Helper::getFormattedDateObject($asset->created_at, 'datetime'), 'updated_at' => Helper::getFormattedDateObject($asset->updated_at, 'datetime'), 'last_audit_date' => Helper::getFormattedDateObject($asset->last_audit_date, 'datetime'), diff --git a/app/Models/Asset.php b/app/Models/Asset.php index 71e32bf872..2b0f3e77be 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -1760,6 +1760,20 @@ class Asset extends Depreciable } + /** + * Query builder scope to order on created_by name + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderByCreatedByName($query, $order) + { + return $query->leftJoin('users as admin_sort', 'assets.created_by', '=', 'admin_sort.id')->select('assets.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order); + } + + /** * Query builder scope to order on assigned user *