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()之后生成的链接链接到我的项目的根目录而不是当前页面. 例: 我的代码在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; 欢迎任何帮助. 解决方法
我终于在工作了几个小时后修好了!
我发现你可以将路径传递给分页器. 所以我的代码现在看起来像: // Create a paginator instance. $profiles = new Paginator($collection->all(),$perPage,Paginator::resolveCurrentPage(),[ 'path' => Paginator::resolveCurrentPath() ]); 对于感兴趣的人,paginator构造函数如下所示: __construct($items,$currentPage = null,array $options = []) 你需要手动传递这个很奇怪,我认为这是一个错误. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |