vipul sorathiya
vipul sorathiya

Reputation: 1316

Pagination with array not working in laravel 5.1

I have to set pagination in array result.

Here is my code.

My Controller code

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;

public function getCVList(){
.
.
.
$jobseeker1 = array_merge($jobseekers, $apps_array);
// in $jobseeker1 there are 6 result.
$jobseeker = $this->paginate($jobseeker1, 3);
return view('frontend.CVList', compact('jobseeker'));
}


public function paginate($items, $perPage, $pageStart = 1) {

    $offSet = ($pageStart * $perPage) - $perPage;

    // Get only the items you need using array_slice
    $itemsForCurrentPage = array_slice($items, $offSet, $perPage, true);

    return new LengthAwarePaginator($itemsForCurrentPage, count($items), $perPage, Paginator::resolveCurrentPage(), array('path' => Paginator::resolveCurrentPath()));
 }

In blade temoplate i used rander() method and thare are also display pagination. But in all page display same record.

Thanks....

Upvotes: 0

Views: 339

Answers (1)

manix
manix

Reputation: 14747

This is because you are not reading the page number clicked in the paginator, you are setting "3" always as the page to display. Try this:

//include the request
use Illuminate\Http\Request;

Now, read the current page:

public function getCVList(Request $request){
    $perPage = 3;

    // read the page number. When page number is not presented, then you 
    // set it as 0
    $page = $request->get('page', 0);
    $page = ($page == 0)? ($page * $perPage) : ($page * $perPage) - $perPage;

    // now, calling the paginator do magic dynamically
    $jobseeker = $this->paginate($jobseeker1, $perPage, $page);

Upvotes: 1

Related Questions