3 php laravel-5 laravel-pagination
我目前正在从4.2中将我的一个项目更新到Laravel 5.我知道paginator类已经发生了很多变化,但我真的无法弄清楚为什么这不起作用.我在我项目中多个位置的雄辩模型上调用paginate(),每个东西都很棒.
但是这个项目有一个带有过滤器的配置文件搜索页面,所以我必须调用一个巨大的自定义DB :: table()查询.之后我想从结果中构建一个paginator对象.
$q = \DB:: HUGE QUERY HERE....
// Execute query
$results = $q->get();
// Get pagination information and slice the results.
$perPage = 20;
$total = count($results);
$start = (Paginator::resolveCurrentPage() - 1) * $perPage;
$sliced = array_slice($results, $start, $perPage);
// Eager load the relation.
$collection = Profile::hydrate($sliced);
$collection->load(['sports', 'info', 'profileImage']);
// Create a paginator instance.
$profiles = new Paginator($collection->all(), $total, $perPage);
return $profiles;
Run Code Online (Sandbox Code Playgroud)
我的问题是调用$profiles->render()链接到我的项目的根目录而不是当前页面后生成的链接.
示例:链接位于mysite.com/profiles但链接到mysite.com/?page=2而不是mysite.com/profiles?page=2
我的代码在Laravel 4.2中运行得很好,下面链接它以供参考:
Laravel 4.2代码有效:
$q = \DB:: HUGE QUERY HERE....
// Execute query
$results = $q->get();
// Get pagination information and slice the results.
$perPage = 20;
$total = count($results);
$start = (Paginator::getCurrentPage() - 1) * $perPage;
$sliced = array_slice($results, $start, $perPage);
// Eager load the relation.
$collection = Profile::hydrate($sliced);
$collection->load(['sports', 'info', 'profileImage']);
// Create a paginator instance.
$profiles = Paginator::make($collection->all(), $total, $perPage);
return $profiles;
Run Code Online (Sandbox Code Playgroud)
欢迎任何帮助.谢谢!
小智 11
我终于在工作了几个小时后修好了!
我发现你可以将路径传递给分页器.正如您所看到的,这可以通过在构建paginator对象时将数组作为第5个参数传递来完成.此数组将覆盖您传递的任何选项.我们需要覆盖该path选项.我正在使用当前路径Paginator::resolveCurrentPath().
所以我的代码现在看起来像:
// Create a paginator instance.
$profiles = new Paginator($collection->all(), $total, $perPage, Paginator::resolveCurrentPage(), [
'path' => Paginator::resolveCurrentPath()
]);
Run Code Online (Sandbox Code Playgroud)
对于感兴趣的人,paginator构造函数如下所示:
__construct($items, $total, $perPage, $currentPage = null, array $options = [])
Run Code Online (Sandbox Code Playgroud)
你需要手动传递这个很奇怪,我认为这是一个错误.
| 归档时间: |
|
| 查看次数: |
4344 次 |
| 最近记录: |