加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 19:35:41 所属栏目:大数据 来源:网络整理
导读:我目前正在从4.2中将我的一个项目更新到Laravel 5.我知道paginator类已经发生了很多变化,但我真的无法弄清楚为什么这不起作用.我在我项目中多个位置的雄辩模型上调用paginate(),每个东西都很棒. 但是这个项目有一个带有过滤器的配置文件搜索页面,所以我必须
我目前正在从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 = [])

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读