laravelpagination

Solutions on MaxInterview for laravelpagination by the best coders in the world

showing results for - "laravelpagination"
Cristina
24 Mar 2017
1<?php namespace App\Http\Controllers;
2
3use Illuminate\Pagination\LengthAwarePaginator;
4use Illuminate\Support\Collection;
5
6class MemberController extends Controller {
7
8    public function search(Request $request)
9    {       
10        $queryResults = [
11            'item1',
12            'item2',
13            'item3',
14            'item4',
15            'item5',
16            'item6',
17            'item7',
18            'item8',
19            'item9',
20            'item10'
21            ];
22
23        //Get current page form url e.g. &page=6
24        $currentPage = LengthAwarePaginator::resolveCurrentPage();
25
26        //Create a new Laravel collection from the array data
27        $collection = new Collection($queryResults);
28
29        //Define how many items we want to be visible in each page
30        $perPage = 5;
31
32        //Slice the collection to get the items to display in current page
33        $currentPageSearchResults = $collection->slice(($currentPage - 1) * $perPage, $perPage)->all();
34
35        //Create our paginator and pass it to the view
36        $paginatedSearchResults= new LengthAwarePaginator($currentPageSearchResults, count($collection), $perPage);
37        $paginatedSearchResults->setPath($request->url());
38        $paginatedSearchResults->appends($request->except(['page']));
39
40        return view('search', ['results' => $paginatedSearchResults]);
41    }
42?>