【问题标题】:Pagination with array not working in laravel 5.1数组分页在 laravel 5.1 中不起作用
【发布时间】:2016-07-02 09:35:39
【问题描述】:

我必须在数组结果中设置分页。

这是我的代码。

我的控制器代码

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()));
 }

在刀片模板中,我使用了 rander() 方法,并且也使用了显示分页。但在所有页面显示相同的记录。

谢谢……

【问题讨论】:

    标签: laravel laravel-4 laravel-5 pagination laravel-5.1


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 2013-09-09
      • 2014-08-15
      • 2016-01-28
      相关资源
      最近更新 更多