Rey Norbert Besmonte
Rey Norbert Besmonte

Reputation: 791

Laravel custom paginate not working

I am having a problem in pagination on laravel.

This was working before with no problem.

$users = user::getAllUsers()->paginate(10);

But I need to change the code therefore the only way is to use the custom pagination

use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Input;

public function companies()
{
     $users = user::getAllUsers()->get(//some codes here);
     $paginated = = new Paginator($users , 2);

     $paginated ->setPath('/users/view');
     return view('users.view', compact('paginated'));
}

The problem with the 2nd code is that when I am pressing the next button to redirect to page=2, the values in the view is not changing. the url is changing though.

Not sure if I am missing something here. Help please

Upvotes: 0

Views: 1980

Answers (2)

Vahid
Vahid

Reputation: 950

Use LengthAwarePaginator

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Http\Request;

public function companies(Request $request){
    ...
    $items_per_page = 2;
    $paginated = new LengthAwarePaginator(
        $users->forPage($request->page ?: 1, $items_per_page),
        $users->count(),
        $items_per_page
    );
    ...
}

Upvotes: 2

Jithin Jose
Jithin Jose

Reputation: 1821

You need to pass it as the third parameter

$paginated = new Paginator($users , 2, $request->get('page'));

Parameters are in the order: items, perPage, currentPage

Upvotes: 1

Related Questions