Created
December 22, 2020 11:12
-
-
Save verschuur/0f1361d58f4f94d1bb5c3473867b27aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Pagination\LengthAwarePaginator; | |
// given the following json response from an API call | |
$json = { | |
"data": [ | |
{ | |
"id": 1, | |
"created_at": "2020-01-01 00:00:00", | |
"updated_at": "2020-01-01 00:00:00" | |
} | |
], | |
"links": { | |
"first": "http://example.org/api/posts?page=1", | |
"last": "http://example.org/api/posts?page=10", | |
"prev": "http://example.org/api/posts?page=4", | |
"next": "http://example.org/api/posts?page=6" | |
}, | |
"meta": { | |
"current_page": 5, | |
"from": 5, | |
"last_page": 10, | |
"path": "http://example.org/api/posts", | |
"per_page": 1, | |
"to": 5, | |
"total": 10 | |
} | |
}" | |
$data = collect(json_decode($json, true)); | |
$posts = collect($data['data']); | |
$links = $data['links']; | |
$meta = $data['meta']; | |
$paginator = new LengthAwarePaginator( | |
$posts, | |
$meta['total'], | |
$meta['per_page'], | |
$meta['current_page'], | |
['path' => request()->url(), 'query' => request()->query()] | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment