Skip to content

Commit a564b27

Browse files
committed
updated getPagination method on MsGraph.php
1 parent d3fc1fc commit a564b27

File tree

2 files changed

+65
-16
lines changed

2 files changed

+65
-16
lines changed

changelog.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,41 @@ MsGraphAdmin::events()->userid($userId)->find($eventId);
233233
MsGraphAdmin::events()->userid($userId)->store($data);
234234
MsGraphAdmin::events()->userid($userId)->update($data);
235235
MsGraphAdmin::events()->userid($userId)->delete($data);
236+
```
237+
238+
### 3.1.0
239+
240+
Changed getPagination() to return array containing only previous and next page numbers.
241+
242+
This method needs the data but also the total number of records, the limit ($top) and the offset ($skip)
243+
244+
```php
245+
$limit = 5;
246+
$skip = request('next', 0);
247+
248+
$messageQueryParams = [
249+
"\$orderby" => "displayName",
250+
"\$count" => "true",
251+
"\$skip" => $skip,
252+
"\$top" => $limit,
253+
];
254+
255+
$contacts = MsGraph::get('me/contacts?'.http_build_query($messageQueryParams));
256+
$total = $contacts['@odata.count'] ?? 0;
257+
258+
$response = MsGraph::getPagination($contacts, $total, $limit, $skip);
259+
$previous = $response['previous'];
260+
$next = $response['next'];
261+
```
262+
263+
The in a view the previous and next links can be displayed:
264+
265+
```php
266+
@if (request('next') > 0)
267+
<a href='{{ url()->current().'?next='.$previous }}'>Previous Page</a>
268+
@endif
269+
270+
@if ($next != 0)
271+
<a href='{{ url()->current().'?next='.$next }}'>Next Page</a>
272+
@endif
236273
```

src/MsGraph.php

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -291,34 +291,46 @@ protected function guzzle($type, $request, $data = [], $headers = [], $id = null
291291
}
292292

293293
/**
294-
* return array containing total, top and skip params
294+
* return array containing previous and next page counts
295295
* @param $data array
296-
* @param $top integer
296+
* @param $total array
297+
* @param $limit integer
297298
* @param $skip integer
298299
* @return array
299300
*/
300-
public function getPagination($data, $top, $skip)
301+
public function getPagination(array $data, int $total, int $limit, int $skip)
301302
{
302-
if (! is_array($data))
303-
{
304-
dd($data);
305-
}
306-
307-
$total = isset($data['@odata.count']) ? $data['@odata.count'] : 0;
303+
$previous = 0;
304+
$next = 0;
308305

309306
if (isset($data['@odata.nextLink'])) {
307+
$parts = explode('skip=', $data['@odata.nextLink']);
308+
309+
if (isset($parts[1])) {
310+
$previous = $parts[1] - $limit;
311+
$next = $parts[1];
312+
}
313+
314+
if ($previous < 0) {
315+
$previous = 0;
316+
}
310317

311-
$parts = parse_url($data['@odata.nextLink']);
312-
parse_str($parts['query'], $query);
318+
if ($next == $total) {
319+
$next = 0;
320+
}
321+
}
322+
323+
if ($total > $limit) {
324+
$previous = $skip - $limit;
325+
}
313326

314-
$top = isset($query['$top']) ? $query['$top'] : 0;
315-
$skip = isset($query['$skip']) ? $query['$skip'] : 0;
327+
if ($previous < 0) {
328+
$previous = 0;
316329
}
317330

318331
return [
319-
'total' => $total,
320-
'top' => $top,
321-
'skip' => $skip
332+
'previous' => $previous,
333+
'next' => $next
322334
];
323335
}
324336
}

0 commit comments

Comments
 (0)