Skip to content

A free open source IT asset/license management system

Added paginateCollection() helper

snipe000bf71342d4
diff --git a/app/Helpers/Helper.php b/app/Helpers/Helper.php
index 933f76e6b0..010cacb87b 100644
--- a/app/Helpers/Helper.php
+++ b/app/Helpers/Helper.php
@@ -18,6 +18,8 @@ use App\Models\User;
use Carbon\Carbon;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Http\RedirectResponse;
+use Illuminate\Pagination\LengthAwarePaginator;
+use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
@@ -100,6 +102,29 @@ class Helper
}
}
+ /**
+ * Slice an in-memory Collection into a LengthAwarePaginator using the
+ * page + perPage values normalized by SetPaginationDefaults middleware.
+ *
+ * Selectlist endpoints that fetch the full set up-front (so they can
+ * post-process — e.g. hierarchy indenting in Companies / Locations) need
+ * to wrap the result in a paginator before handing it to SelectlistTransformer.
+ * This helper avoids re-deriving the page math at every call site, and
+ * inherits the middleware's bounds check against config('app.max_results').
+ */
+ public static function paginateCollection(Collection $items): LengthAwarePaginator
+ {
+ $page = app('api_current_page');
+ $perPage = app('api_limit_value');
+
+ return new LengthAwarePaginator(
+ $items->forPage($page, $perPage)->values(),
+ $items->count(),
+ $perPage,
+ $page,
+ );
+ }
+
public static function parseEscapedMarkedownInline($str = null)
{
$Parsedown = new \Parsedown;