Question

For simplicities sake, I have an array, $matches, containing my results in my controller. Normally, I would return a view, passing that array along.

return View::make('results', compact('matches'));

However, I want to paginate these results in the view.

@foreach ($matches as $match)
   ...
@endforeach
// Paginate matches here!

It seems like Laravel's built in pagination won't work for arrays. I can't find any resource that shows me how to do this. Any help would be appreciated!

Was it helpful?

Solution

I ended up using the following code. You have to pass the items per page manually. $matches is the array of data being passed to the paginator.

$perPage = 2;
$currentPage = Input::get('page') - 1;
$pagedData = array_slice($matches, $currentPage * $perPage, $perPage);
$matches = Paginator::make($pagedData, count($matches), $perPage);

OTHER TIPS

You can create paginators manually and they work with arrays:

$paginator = Paginator::make($items, $totalItems, $perPage);

Check the docs: http://laravel.com/docs/pagination

If you want to manually paginate this is the best answer. This is usefull when your query contain distinct().

$matches = DB::table('...')->where('...')->distict()->get();    
$perPage = 2;
$currentPage = Input::get('page', 1) - 1;
$pagedData = array_slice($matches, $currentPage * $perPage, $perPage);
$matches = Paginator::make($pagedData, count($matches), $perPage);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top