0
votes

I am working on two paginations on one page and links of pagination are not working properly.

The situation is following: I get an Eloquent object, and then sort it and do some process, whereby I convert it to two separate collections.

Now I have two collections which I have transfered by "Lengthawarepaginator" - and added a pagination name for each by method "setPageName()". (I have to do this by custom paginate, since I am working with those, so more specific Eloquent query is out of question).

Everything is working perfectly; when I click on any paginate link of any of those two paginations - it shows proper paginated results for that pagination, simply perfect.

But, pagination links are not working in proper display :

When I click on page 2,3,4, and so forth, even though I get displayed proper page of pagination, the pagination link stays ALWAYS at number one - appearing like it is displaying page one of pagination (even displaying for example 2,3,4 and so forth of the pagination page results). Simply put, the pagination links don't follow the appropriate pagination page shown. Plus, a link to page 1 of pagination is not clickable at all.

Here is my controller code:

    $currentPage=LengthAwarePaginator::resolveCurrentPage('popular')-1;
    $perPage=10;
    $currentPageBlogResults = $items1->slice($currentPage * $perPage, $perPage)->all();
    $items1= new LengthAwarePaginator($currentPageBlogResults, count($items1), $perPage);
    $items1->setPath('offers');
    $items1->setPageName('popular');


    $currentPage=LengthAwarePaginator::resolveCurrentPage('highlow')-1;
    $perPage=10;
    $currentPageBlogResults = $items2->slice($currentPage * $perPage, $perPage)->all();
    $items2= new LengthAwarePaginator($currentPageBlogResults, count($items2), $perPage);
    $items2->setPath('offers');
    $items2->setPageName('highlow');

For the view: I simply loop over the results with foreach and under each I simply echo pagination links like: {{$items1->links() }} and {{$items2->links () }}

Any suggestions?

1

1 Answers

0
votes
$pageNumber  = (!empty($request->p))? $request->p : 1;
$perPage     = 16;
$offset      = ($pageNumber - 1) * $perPage;

$binding = [];
$binding['status'] = 1;
$sqlQuery  = 'SELECT * FROM TABLE_NAME status= :status';

$sql_count = 'SELECT count(1) as total_records FROM ('. $sqlQuery . ') searchedData';
$result = DB::select($sql_count,$binding);
$resultCount = $result[0]->total_records;

$sqlQuery .= ' LIMIT ' . $offset . ', ' . $perPage;
$searchResult = DB::select($sqlQuery,$binding);
$paginator = new \Illuminate\Pagination\LengthAwarePaginator($searchResult, $resultCount, $perPage, $pageNumber);
$paginator->setPath(route('search'));
$paginator->setPageName("p");

Pass current page number in the method, in above example I passed $pageNumber.