Skip to content

Instantly share code, notes, and snippets.

@kundancool
Created May 10, 2018 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kundancool/ef7ed2165ea5c57d87e499f1d328cb5f to your computer and use it in GitHub Desktop.
Save kundancool/ef7ed2165ea5c57d87e499f1d328cb5f to your computer and use it in GitHub Desktop.
Laravel Length Aware Pagination for custom array
<?php
public function sampleArrayPaginator(Request $request) {
$response = array();
$page = ($request->has('page')) ? intval($request->page) : 1;
$size = ($request->has('size')) ? intval($request->size) : 10;
$collection = collect($response);
$total = $collection->count();
return new LengthAwarePaginator(
array_values($collection->forPage($page, $size)->toArray()),
$collection->count(),
$size,
$page
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment