2017-02-15 47 views
0

我试图使用长度意识到分页程序,像这样进行分页集合:Laravel收集forPage删除排序?

new LengthAwarePaginator($cases->forPage($request->get('page'), $per_page), $cases->count(), $per_page, $request->get('page')); 

在此之前,我通过整理收集公平的。

$cases->sortBy('inactive_percentage', SORT_REGULAR, 'desc'); 

但是我从paginator得到的结果是不一样的。我首先得到不活跃的百分比。如果我尝试per_page来说100比存在的记录更多,那么结果会被正确排序。

如何使用paginator来处理排序后的集合?

+0

你真的需要做的'forPage '?我认为这是paginators的工作。 – apokryfos

+0

如果我必须手动使用它,我认为我做 – Rohan

回答

1

我不能发表任何评论...笑试图让我代表了一个小...

我用手动分页程序遭遇结束了我不得不切片阵列自己......作为每该文档:

当手动创建一个分页程序实例,应手动 “切片”的结果的数组传递给分页程序。如果你是 不确定如何做到这一点,请查看array_slice PHP函数。

我的手动分页阵列控制器功能的一个实例(paginators应对潜在阵列...)

public function comments(){ 

     // DB::select returns an array, thus we have to build the paginator ourselves... 
     $comm = DB::select('select bla bla bla... 
          order by c.approved ASC, c.id DESC '); 

     // this basically gets the request's page variable... or defaults to 1 
     $page = Paginator::resolveCurrentPage('page') ?: 1; 

     // Assume 15 items per page... so start index to slice our array 
     $startIndex = ($page - 1) * 15; 

     // Length aware paginator needs a total count of items... to paginate properly 
     $total = count($comm); 

     // Eliminate the non relevant items... 
     $results = array_slice($comm, $startIndex, 15); 

     $comments = new LengthAwarePaginator($results, $total, 15, $page, [ 
      'path' => Paginator::resolveCurrentPath(), 
      'pageName' => 'page', 
     ]); 
     return view('backend/comments', compact('comments')); 
    } 

希望这有助于...

+0

是的,我必须自己拼接它,以便您的答案是正确的。谢谢 – Rohan