Added searching, sorting, etc to locations API

This commit is contained in:
snipe 2017-01-13 07:40:08 -08:00
parent fbbf5e77b4
commit 295744be64
2 changed files with 31 additions and 6 deletions

View file

@ -17,11 +17,38 @@ class LocationsController extends Controller
* @since [v4.0]
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
$this->authorize('view', Location::class);
$locations = Location::all();
return (new DatatablesTransformer)->transformDatatables($locations, count($locations));
$allowed_columns = ['id','name','address','address2','city','state','country','zip'];
$locations = Location::select([
'locations.id',
'locations.name',
'locations.address',
'locations.address2',
'locations.city',
'locations.state',
'locations.zip',
'locations.country',
'locations.parent_id',
'locations.currency'
])->withCount('assets')->withCount('users');
if ($request->has('search')) {
$locations = $locations->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';
$locations->orderBy($sort, $order);
$total = $locations->count();
$locations = $locations->skip($offset)->take($limit)->get();
return (new DatatablesTransformer)->transformDatatables($locations, $total);
}

View file

@ -22,8 +22,6 @@ class SuppliersController extends Controller
$this->authorize('view', Supplier::class);
$allowed_columns = ['id','name','address','phone','contact','fax','email'];
// Not sure how to access the withCount value?
$suppliers = Supplier::select(
array('id','name','address','address2','city','state','country','fax', 'phone','email','contact')
)->withCount('assets')->withCount('licenses')->whereNull('deleted_at');