Last active
December 4, 2019 10:31
-
-
Save RafikFarhad/7af117052b1237b725e03eb0d6c65699 to your computer and use it in GitHub Desktop.
Get Paginated info from any Eloquent ORM Query
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
public function mergeQueryPaginate(\Illuminate\Database\Eloquent\Builder $query): \Illuminate\Pagination\LengthAwarePaginator | |
{ | |
$raw_query = $query; | |
$totalCount = $raw_query->get()->count(); | |
$page = request('page', 1); | |
$skip = $perPage * ($page - 1); | |
$raw_query = $raw_query->take($perPage)->skip($skip); | |
$parameters = request()->getQueryString(); | |
$parameters = preg_replace('/&page(=[^&]*)?|^page(=[^&]*)?&?/', '', $parameters); | |
$path = url(request()->getPathInfo() . '?' . $parameters); | |
$rows = $raw_query->get(); | |
$paginator = new LengthAwarePaginator($rows, $totalCount, $perPage, $page); | |
$paginator = $paginator->withPath($path); | |
return $paginator; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Details