php – Laravel 5 Pagination链接到错误的地方

前端之家收集整理的这篇文章主要介绍了php – Laravel 5 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;

我的问题是,在调用$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,'profileImage']);

// Create a paginator instance.
$profiles = Paginator::make($collection->all(),$perPage);

return $profiles;

欢迎任何帮助.
谢谢!

解决方法

我终于在工作了几个小时后修好了!

我发现你可以将路径传递给分页器.
正如您所看到的,这可以通过在构建paginator对象时将数组作为第5个参数传递来完成.此数组将覆盖您传递的任何选项.我们需要覆盖路径选项.我正在使用Paginator :: resolveCurrentPath()获取当前路径.

所以我的代码现在看起来像:

// Create a paginator instance.
$profiles = new Paginator($collection->all(),$perPage,Paginator::resolveCurrentPage(),[
    'path' => Paginator::resolveCurrentPath()
]);

对于感兴趣的人,paginator构造函数如下所示:

__construct($items,$currentPage = null,array $options = [])

你需要手动传递这个很奇怪,我认为这是一个错误.

猜你在找的Laravel相关文章