我必须在数组结果中设置分页。
这是我的代码。
我的控制器代码
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()));
}
在刀片temoplate我使用rander()方法和thare也显示分页。但在所有页面显示相同的记录。
谢谢....
答案 0 :(得分:1)
这是因为您没有阅读在分页器中点击的页码,您正在设置" 3"始终作为要显示的页面。试试这个:
//include the request
use Illuminate\Http\Request;
现在,请阅读当前页面:
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);