Skip to content

Instantly share code, notes, and snippets.

@Ellrion
Created June 27, 2018 07:51
Show Gist options
  • Save Ellrion/862f2563715f95d5e5ba1e88329c5fb3 to your computer and use it in GitHub Desktop.
Save Ellrion/862f2563715f95d5e5ba1e88329c5fb3 to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Support\Collection;
Collection::macro('crossMerge', function (Collection $collection) {
return $this->reduce(function (Collection $result, $item) use ($collection) {
$result->push($item);
if ($collection->isNotEmpty()) {
$result->push($collection->shift());
}
return $result;
}, new Collection())->concat($collection);
});
Collection::macro('paginate', function ($perPage = 25, $pageName = 'page', $currentPage = null) {
$currentPage = $currentPage ?: \Illuminate\Pagination\Paginator::resolveCurrentPage($pageName);
$currentPage = $currentPage ?: 1;
return new \Illuminate\Pagination\LengthAwarePaginator(
$this->forPage($currentPage, $perPage)->values(),
$this->count(),
$perPage,
$currentPage,
['path' => \Illuminate\Pagination\Paginator::resolveCurrentPath()]
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment