Skip to content

Commit cb6e7f7

Browse files
authored
Merge pull request #16561 from spencerrlongg/bug/api_get_by_serial_add_pagination
Adds Pagination to Hardware By Serial API Request
2 parents c305284 + bfd827e commit cb6e7f7

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

app/Http/Controllers/Api/AssetsController.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,15 +491,32 @@ public function showByTag(Request $request, $tag): JsonResponse | array
491491
public function showBySerial(Request $request, $serial): JsonResponse | array
492492
{
493493
$this->authorize('index', Asset::class);
494-
$assets = Asset::where('serial', $serial)->with('assetstatus')->with('assignedTo');
494+
$assets = Asset::where('serial', $serial)->with([
495+
'assetstatus',
496+
'assignedTo',
497+
'company',
498+
'defaultLoc',
499+
'location',
500+
'model.category',
501+
'model.depreciation',
502+
'model.fieldset',
503+
'model.manufacturer',
504+
'supplier',
505+
]);
495506

496507
// Check if they've passed ?deleted=true
497508
if ($request->input('deleted', 'false') == 'true') {
498509
$assets = $assets->withTrashed();
499510
}
500511

501-
if (($assets = $assets->get()) && ($assets->count()) > 0) {
502-
return (new AssetsTransformer)->transformAssets($assets, $assets->count());
512+
$offset = ($request->input('offset') > $assets->count()) ? $assets->count() : app('api_offset_value');
513+
$limit = app('api_limit_value');
514+
515+
$total = $assets->count();
516+
$assets = $assets->skip($offset)->take($limit)->get();
517+
518+
if (($assets) && ($assets->count()) > 0) {
519+
return (new AssetsTransformer)->transformAssets($assets, $total);
503520
}
504521

505522
// If there are 0 results, return the "no such asset" response

0 commit comments

Comments
 (0)