Skip to content

updated getPagination method on MsGraph.php

Choose a tag to compare

@dcblogdev dcblogdev released this 28 Aug 21:40
· 219 commits to master since this release

Changed getPagination() to return array containing only previous and next page numbers.

This method needs the data but also the total number of records, the limit ($top) and the offset ($skip)

$limit = 5;
$skip  = request('next', 0);

$messageQueryParams = [
	"\$orderby" => "displayName",
	"\$count"   => "true",
	"\$skip"    => $skip,
	"\$top"     => $limit,
];

$contacts = MsGraph::get('me/contacts?'.http_build_query($messageQueryParams));
$total    = $contacts['@odata.count'] ?? 0;

$response = MsGraph::getPagination($contacts, $total, $limit, $skip);
$previous = $response['previous'];
$next     = $response['next'];

The in a view the previous and next links can be displayed:

@if (request('next') > 0)
	<a href='{{ url()->current().'?next='.$previous }}'>Previous Page</a>
@endif

@if ($next != 0)
	<a href='{{ url()->current().'?next='.$next }}'>Next Page</a>
@endif