2014-11-08 64 views
0

我试图在laravel.Checking的laravel文档创建原始查询手动分页,我看到使用方法分页程序,如:`原始查询的手动分页laravel

$申请人=分页程序::使($申请人,$ totalApplicants,4);`

但是,当我返回$申请人它给我所有的记录,而不是我的Paginator :: make方法所要求的4,但如果我打电话给我的申请人的链接()mehod像:$ applicants-> links()它返回到我的视图的分页链接

请问我做错了什么,或者我需要写一个mehod使这项工作

从我的控制器代码:

$statement3 = "SELECT * FROM applicant where lga = '$appulga' "; 

     //loop through lga id to build query 
     foreach($LGA_Names as $lga) 
     { 
      $statement3.= " "."OR"." "."lga=". "'".$lga."'"; 
     } 

     //FAcility for User assigned LGA 
     $applicants = DB::Select($statement3); 
     /////////////////////////////////////////////////////////////// 
     $totalApplicants = count($applicants); 
     $perpage = 4; 

     $applicants = Paginator::make($applicants,$totalApplicants,$perpage); 
+0

你能分享更多的代码吗? '$ applicants'必须是数组 – knpsck 2014-11-08 13:32:05

+0

只是从我的控制器 – 2014-11-08 13:38:03

回答

1

分页程序不分裂的集合,你必须手动处理结果。

$page = Paginator::getCurrentPage(); // Get the current page for the request 

$limit = 4; 
$offset = $page * $limit; 

// query to get `COUNT(*)` 
// query to get results with `LIMIT $offset, $limit` 

Paginator::make($results, $total, $limit); 

手动编写sql查询手动无效且不安全,改为使用查询生成器;

$query = DB::table('applicant')->where('lga', $appulga); 

foreach ($LGA_Names as $lga) 
{ 
    $query->orWhere('lga', $lga); 
} 

$applicants = $query->paginate(4); 
+0

共享代码实际上我的查询比你看到它更复杂。我必须根据用户登录ID生成lga,然后从设施数据库生成设施,然后从申请人表中生成最终申请人。所以使用雄辩的查询是我的选择 – 2014-11-08 14:39:38

1

你已经获得了所有记录,因为你叫Paginator::make$applicants,您可以尝试这样的事:

$pageNumber = Input::get('page', 1); 
$perpage = 4; 


$statement = "SELECT * FROM applicant where lga = '$appulga' "; 
// ... 
$applicants = DB::Select($statement); 


$slice = array_slice($applicants, $perpage * ($pageNumber - 1), $perpage); 
$applicants = Paginator::make($slice, count($applicants), $perpage); 
1

分页程序计算分页的链接,所以它不会影响您的查询。

它只返回/Illuminate/Pagination/Paginator对象,如果你想返回作为响应或传递来查看,它将返回在toArray方法中定义的数组元素。