如何在laravel中使用pagination :: make()来显示视图中的结果集?

时间:2014-05-26 12:45:01

标签: laravel laravel-4 pagination

我使用Paginator::make对表中的记录进行了分页。在视图中,我正在获取分页链接,但每个链接都包含其中的所有记录。如何将其限制为perPage个项目?

    $datas = Paginator::make($paginator, count($paginator), $perPage);
    return $datas;

代码输出:

{"total":10,"per_page":5,"current_page":1,"last_page":2,"from":1,"to":5,"data":      
[{"id":"10","languages":"ds","created_at":"2014-05-23 
11:59:02.000","created_by":"1","updated_at":"2014-05-23 
11:59:02.000","updated_by":"1","is_active":"1"},
{"id":"9","languages":"urdu","created_at":"2014-05-23 
11:57:24.000","created_by":"1","updated_at":"2014-05-23 
11:57:24.000","updated_by":"1","is_active":"1"},
{"id":"8","languages":"were","created_at":"2014-05-23 
11:55:49.000","created_by":"1","updated_at":"2014-05-23 
11:55:49.000","updated_by":"1","is_active":"1"},  
{"id":"7","languages":"delete","created_at":"2014-05-23  
11:54:57.000","created_by":"1","updated_at":"2014-05-24  
06:02:46.000","updated_by":"1","is_active":"1"},
{"id":"6","languages":"sdf","created_at":"2014-05-23 
11:53:11.000","created_by":"1","updated_at":"2014-05-23 
11:53:11.000","updated_by":"1","is_active":"1"},
{"id":"5","languages":"dada","created_at":"2014-05-23 
11:51:33.000","created_by":"1","updated_at":"2014-05-24 
05:44:34.000","updated_by":"1","is_active":"1"},
{"id":"4","languages":"English","created_at":"2014-05-23 
11:49:49.000","created_by":"1","updated_at":"2014-05-23 
11:49:49.000","updated_by":"1","is_active":"1"},
{"id":"3","languages":"asdfgf","created_at":"2014-05-23 
11:48:20.000","created_by":"1","updated_at":"2014-05-23 
11:48:20.000","updated_by":"1","is_active":"1"},
{"id":"2","languages":"Tamil","created_at":"2014-05-23 
10:55:50.000","created_by":"1","updated_at":"2014-05-23 
10:55:50.000","updated_by":"1","is_active":"1"},
{"id":"1","languages":"Tamil","created_at":"2014-05-23 
10:51:42.000","created_by":"1","updated_at":"2014-05-26 
04:41:27.000","updated_by":"1","is_active":"1"}]}

3 个答案:

答案 0 :(得分:4)

实际上Paginator :: make函数我们只需传递所需的值而不是所有值。因为paginator :: make函数只显示发送给它的数据。要将正确的偏移分页数据发送到paginator :: make,应遵循以下方法

    $paginator = json_decode($response);
    $perPage = 5;   
    $page = Input::get('page', 1);
    if ($page > count($paginator) or $page < 1) { $page = 1; }
    $offset = ($page * $perPage) - $perPage;
    $articles = array_slice($paginator,$offset,$perPage);
    $datas = Paginator::make($articles, count($paginator), $perPage);

希望这会对某人有所帮助......

答案 1 :(得分:3)

Paginator::make()

此功能仅创建分页系统。要限制每个页面的数据,您必须对查询进行一些更改。这是一个例子。

        $pageNo = Input::get('page', 1);
        $perPage = 10;
        $from = $pageNo*$perPage-$perPage;
        $to = $perPage;

        $data['allData'] = DB::select( DB::raw('SELECT * FROM tablename LIMIT '.$from.','.$to));
        $totalData = DB::select( DB::raw('SELECT * FROM tablename'));
        $data['paginator'] = Paginator::make($data['allData'], count($totalData), $perPage);

然后您可以将数据显示到您的视图页面

答案 2 :(得分:0)

您应加载view并使用以下内容将$datas传递给view

return View::make('viewname')->with('datas', $datas);

然后在view循环$datas中的所有模型并打印出links,例如:

foreach($datas as $data) {
    echo $data['propertyname']; // $data['username'];
}

最后打印链接:

echo $data->links();

如果您使用Blade,请使用{{ $data['propertyname'] }}{{ $data->links() }}代替echo,同时更改foreachcheck here for more。另外,请确保您在viewname中使用了正确的make(...),因为viewname方法中传递的make(...)默认情况下会从app/views文件夹中加载。