Laravel 5 Pagination with array

Laravel provides pagination for Eloquent results out of the box, but for custom data, we need to do the pagination manually.

In this tutorial, I’ll use Laravel5 LengthAwarePaginator class to show you how you can create a paginated list of results from an array of data.

Here is the Script –

Controller – app/Http/Controller/ItemsController.php

slice(($currentPage * $perPage) - $perPage, $perPage)->all();
        // Create our paginator and pass it to the view
        $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage);

        // set url path for generted links
        return view('items_view', ['items' => $paginatedItems]);


Here is the view file


Items List

    @foreach ($items as $item)
  • {{ $item }}
  • @endforeach
{{ $items->links() }}

Define your route as shown below –

Route::get('items','[email protected]');

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems. Protection Status