如何在 Laravel 中使用 paginator::make() 在视图中显示结果集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23870493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use paginator::make() in laravel to show resultset in view?
提问by uma
I have used Paginator::make
to paginate the records in the table. In the view, I am getting the pagination links, but every link has all the records in it. How to restrict it to perPage
items?
我曾经Paginator::make
对表中的记录进行分页。在视图中,我正在获取分页链接,但每个链接都包含其中的所有记录。如何将其限制为perPage
项目?
$datas = Paginator::make($paginator, count($paginator), $perPage);
return $datas;
The code outputs:
代码输出:
{"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"}]}
回答by uma
Actually Paginator::make function we need to pass only the required values instead of all values. Because paginator::make function simply displays the data send to it. To send the correct offset paginated data to the paginator::make, the following method should be followed
实际上,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);
Hope this will help someone...
希望这会帮助某人...
回答by BlackHyman
Paginator::make()
Paginator::make()
This function only creates the paging system. To restrict your data for each page you have to do some changes to your query. Here is an example.
此功能仅创建分页系统。要限制每个页面的数据,您必须对查询进行一些更改。这是一个例子。
$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);
Then you can show the data to your view page
然后您可以将数据显示到您的查看页面
回答by The Alpha
You should load a view
and pass the $datas
to that view
using something like this:
您应该加载 aview
并将其传递$datas
给view
使用以下内容的内容:
return View::make('viewname')->with('datas', $datas);
Then in your view
loop all the models in $datas
and print out the links
, for example:
然后在您的view
循环中输入所有模型$datas
并打印出links
,例如:
foreach($datas as $data) {
echo $data['propertyname']; // $data['username'];
}
Finally print the links:
最后打印链接:
echo $data->links();
If you are using Blade
then use {{ $data['propertyname'] }}
and {{ $data->links() }}
instead of echo
and also change foreach
, check here for more. Also make sure you have used the right viewname
in make(...)
because the viewname
passed in the make(...)
method wull be loaded from app/views
folder by default.
如果您正在使用Blade
再使用{{ $data['propertyname'] }}
和{{ $data->links() }}
替代echo
,改变它foreach
,点击此处了解。还要确保您使用了正确的viewname
inmake(...)
因为方法中viewname
传递的默认情况下make(...)
将从app/views
文件夹加载。