mirror of
https://github.com/snipe/snipe-it.git
synced 2025-01-26 05:01:06 -08:00
46 lines
1.4 KiB
PHP
46 lines
1.4 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers\Api;
|
||
|
|
||
|
use Illuminate\Http\Request;
|
||
|
use App\Http\Controllers\Controller;
|
||
|
use App\Models\Department;
|
||
|
use App\Http\Transformers\DepartmentsTransformer;
|
||
|
|
||
|
class DepartmentsController extends Controller
|
||
|
{
|
||
|
/**
|
||
|
* Display a listing of the resource.
|
||
|
*
|
||
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||
|
* @since [v4.0]
|
||
|
* @return \Illuminate\Http\Response
|
||
|
*/
|
||
|
public function index(Request $request)
|
||
|
{
|
||
|
$this->authorize('view', Department::class);
|
||
|
$allowed_columns = ['id','name'];
|
||
|
|
||
|
$departments = Department::select([
|
||
|
'id',
|
||
|
'name',
|
||
|
'location_id'
|
||
|
])->with('users')->withCount('assets')->withCount('users');
|
||
|
|
||
|
if ($request->has('search')) {
|
||
|
$departments = $departments->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';
|
||
|
$departments->orderBy($sort, $order);
|
||
|
|
||
|
$total = $departments->count();
|
||
|
$departments = $departments->skip($offset)->take($limit)->get();
|
||
|
return (new DepartmentsTransformer)->transformDepartments($departments, $total);
|
||
|
|
||
|
}
|
||
|
}
|