Merge pull request #16561 from spencerrlongg/bug/api_get_by_serial_add_pagination
Adds Pagination to Hardware By Serial API Request
This commit is contained in:
commit
cb6e7f7b6e
1 changed files with 20 additions and 3 deletions
|
@ -491,15 +491,32 @@ class AssetsController extends Controller
|
||||||
public function showBySerial(Request $request, $serial): JsonResponse | array
|
public function showBySerial(Request $request, $serial): JsonResponse | array
|
||||||
{
|
{
|
||||||
$this->authorize('index', Asset::class);
|
$this->authorize('index', Asset::class);
|
||||||
$assets = Asset::where('serial', $serial)->with('assetstatus')->with('assignedTo');
|
$assets = Asset::where('serial', $serial)->with([
|
||||||
|
'assetstatus',
|
||||||
|
'assignedTo',
|
||||||
|
'company',
|
||||||
|
'defaultLoc',
|
||||||
|
'location',
|
||||||
|
'model.category',
|
||||||
|
'model.depreciation',
|
||||||
|
'model.fieldset',
|
||||||
|
'model.manufacturer',
|
||||||
|
'supplier',
|
||||||
|
]);
|
||||||
|
|
||||||
// Check if they've passed ?deleted=true
|
// Check if they've passed ?deleted=true
|
||||||
if ($request->input('deleted', 'false') == 'true') {
|
if ($request->input('deleted', 'false') == 'true') {
|
||||||
$assets = $assets->withTrashed();
|
$assets = $assets->withTrashed();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($assets = $assets->get()) && ($assets->count()) > 0) {
|
$offset = ($request->input('offset') > $assets->count()) ? $assets->count() : app('api_offset_value');
|
||||||
return (new AssetsTransformer)->transformAssets($assets, $assets->count());
|
$limit = app('api_limit_value');
|
||||||
|
|
||||||
|
$total = $assets->count();
|
||||||
|
$assets = $assets->skip($offset)->take($limit)->get();
|
||||||
|
|
||||||
|
if (($assets) && ($assets->count()) > 0) {
|
||||||
|
return (new AssetsTransformer)->transformAssets($assets, $total);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there are 0 results, return the "no such asset" response
|
// If there are 0 results, return the "no such asset" response
|
||||||
|
|
Loading…
Add table
Reference in a new issue