diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php index 7acd3f5976..a4cecc2cc6 100644 --- a/app/Http/Controllers/Api/DepartmentsController.php +++ b/app/Http/Controllers/Api/DepartmentsController.php @@ -22,17 +22,17 @@ class DepartmentsController extends Controller public function index(Request $request) { $this->authorize('view', Department::class); - $allowed_columns = ['id','name','image']; + $allowed_columns = ['id','name','image','users_count']; $departments = Department::select([ - 'id', - 'name', - 'location_id', - 'company_id', - 'manager_id', - 'created_at', - 'updated_at', - 'image' + 'departments.id', + 'departments.name', + 'departments.location_id', + 'departments.company_id', + 'departments.manager_id', + 'departments.created_at', + 'departments.updated_at', + 'departments.image' ])->with('users')->with('location')->with('manager')->with('company')->withCount('users'); if ($request->has('search')) { @@ -43,7 +43,18 @@ class DepartmentsController extends Controller $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'; - $departments->orderBy($sort, $order); + + switch ($request->input('sort')) { + case 'location': + $departments->OrderLocation($order); + break; + case 'manager': + $departments->OrderManager($order); + break; + default: + $departments->orderBy($sort, $order); + break; + } $total = $departments->count(); $departments = $departments->skip($offset)->take($limit)->get(); diff --git a/app/Models/Department.php b/app/Models/Department.php index 87783c182a..1ed97ecc76 100644 --- a/app/Models/Department.php +++ b/app/Models/Department.php @@ -93,5 +93,31 @@ class Department extends SnipeModel } + /** + * Query builder scope to order on location name + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderLocation($query, $order) + { + return $query->leftJoin('locations as department_location', 'departments.location_id', '=', 'department_location.id')->orderBy('department_location.name', $order); + } + + /** + * Query builder scope to order on manager name + * + * @param \Illuminate\Database\Query\Builder $query Query builder instance + * @param text $order Order + * + * @return \Illuminate\Database\Query\Builder Modified query builder + */ + public function scopeOrderManager($query, $order) + { + return $query->leftJoin('users as department_user', 'departments.manager_id', '=', 'department_user.id')->orderBy('department_user.first_name', $order)->orderBy('department_user.last_name', $order); + } + } diff --git a/resources/views/departments/index.blade.php b/resources/views/departments/index.blade.php index df7d313403..05d17dfe15 100644 --- a/resources/views/departments/index.blade.php +++ b/resources/views/departments/index.blade.php @@ -41,9 +41,9 @@