Created
October 5, 2021 06:48
-
-
Save jaeyson/570aa81da0edd534dc2616f5f7f43111 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 | |
namespace App; | |
use Illuminate\Support\Collection; | |
use Illuminate\Database\Eloquent\Builder; | |
class CursorPaginate | |
{ | |
// (new CursorPaginate())->loadMacro(); //AppServiceProvider boot method | |
// e.g. User::where('role', 'admin')->apiCursorPaginate(); | |
public function loadMacro() | |
{ | |
$this->macro(); | |
} | |
public function macro() | |
{ | |
$self = new static; | |
Builder::macro('apiCursorPaginate', function () use ($self){ | |
return $self->mapPaginator($this->cursorPaginate()); | |
}); | |
} | |
public function mapPaginator($paginator) | |
{ | |
return new Collection([ | |
'users' => $paginator->items(), | |
'url' => $paginator->path(), | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment