如果在获取结果集后获取数据的总计数,则Laravel分页将中断。知道为什么吗?
这是有效的。但是,只要我切换$resultSets的顺序,当页码大于1时,生成的计数值就会开始给我0。$ResultSet的切换代码为: 为什么会这样?谢谢。这是因为$profiles数据在对其进行跳过操作后被操纵, 您可以做的是克隆变量,并像这样使用它,使您的计数不受影响如果在获取结果集后获取数据的总计数,则Laravel分页将中断。知道为什么吗?,laravel,laravel-pagination,Laravel,Laravel Pagination,这是有效的。但是,只要我切换$resultSets的顺序,当页码大于1时,生成的计数值就会开始给我0。$ResultSet的切换代码为: 为什么会这样?谢谢。这是因为$profiles数据在对其进行跳过操作后被操纵, 您可以做的是克隆变量,并像这样使用它,使您的计数不受影响 $resultsSet = [ "data" => $profiles->skip( ($params['page_number'] - 1) * $params['page_size']
$resultsSet = [
"data" => $profiles->skip( ($params['page_number'] - 1) * $params['page_size'] )->take($params['page_size'])->get(),
"result_count" => $profiles->count()
];
现在,即使你颠倒顺序,计数也不会受到影响
$profiles = Profile::filter($params);
$temp = clone $profiles // This is will make exact copy of your $profile variable
$resultsSet = [
"result_count" => $temp->count(),
"data" => $profiles->skip( ($params['page_number'] - 1) * $params['page_size'] )->take($params['page_size'])->get()
];
希望我能解释清楚。谢谢。您的回答确实让我更进一步,但它仍然无法解释为什么计数现在为0。我告诉过您,如果将$profiles放在第一位,则会操纵$profiles,如果将$profiles放在@SaketJainYeah之后,则会影响$profiles的计数,我明白了。但是为什么是0呢?我的意思是,它应该减少我跳过的配置文件的数量,这就是我期望的常规行为be@SaketJain您必须验证您的查询,并查看它是否正在执行意外操作
$profiles = Profile::filter($params);
$temp = clone $profiles // This is will make exact copy of your $profile variable
$resultsSet = [
"result_count" => $temp->count(),
"data" => $profiles->skip( ($params['page_number'] - 1) * $params['page_size'] )->take($params['page_size'])->get()
];
$resultsSet = [
"data" => $profiles->skip( ($params['page_number'] - 1) * $params['page_size'] )->take($params['page_size'])->get(),
"result_count" => $temp->count(),
];