Full Stack LAMP - MEAN Developer, Python developer. Certified Azure Developer. Freelance programmer/consultant/trainer.

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
        $paginatedItems->setPath($request->url());
 
        return view('items_view', ['items' => $paginatedItems]);
    }

}

Here is the view file
resources/views/item_view.blade.php

 

Items List

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

Define your route as shown below –
app/Http/Routes.php

Route::get('items','ItemsController@items');
  1. hey can u help me
    how array like this
    $data[‘a’]=DB::select(“select * from table_name);
    $data[‘b’]=DB::select(“select * from table_name);

    return view(‘view.home’,$data);
    how to add pagination like you do it

    1. $data[‘a’] = DB::table(‘table_name’)->select(‘column’)->paginate(3);
      return view(‘view’, [‘data’ => $data]);

      in blade use {{ $data[‘a’]->links() }}

Leave a Reply