Created
November 15, 2020 17:26
-
-
Save gghughunishvili/8e9f327659061bc91c89d77036bc02a7 to your computer and use it in GitHub Desktop.
Change Laravel pagination structure in API Resources
This file contains hidden or 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 | |
namespace App\Http\Resources; | |
use Illuminate\Http\Resources\Json\ResourceCollection; | |
class BaseCollection extends ResourceCollection | |
{ | |
public function withResponse($request, $response) | |
{ | |
$data = json_decode($response->getContent(), true); | |
if (isset($data['meta']) && isset($data['meta']['per_page'])) { | |
$data['pagination'] = [ | |
'total' => $data['meta']['total'], | |
'per_page' => $data['meta']['per_page'], | |
'current_page' => $data['meta']['current_page'], | |
'last_pages' => $data['meta']['last_page'], | |
]; | |
unset($data['links'], $data['meta']); | |
} | |
$response->setContent(json_encode($data)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment