【发布时间】:2015-06-05 06:13:07
【问题描述】:
我目前正在将我的一个项目从 4.2 更新到 Laravel 5。我知道分页器类发生了很多变化,但我真的不知道为什么这不起作用。我在项目中的多个位置对 eloquent 模型调用 paginate() 并且一切都很好。
但是同一个项目有一个带有过滤器的配置文件搜索页面,所以我必须调用一个巨大的自定义 DB::table() 查询。之后我想从结果中构建一个分页器对象。
$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, $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;
欢迎任何帮助。 谢谢!
【问题讨论】:
-
mysite.com\?page=2这里的正斜杠是正确的还是错字? -
糟糕,这是一个错字,我的意思是 /
-
我想知道
str_replace在这里是否适合您,例如:str_replace('/?', '?', $profiles->render()); -
这仍将链接到主页而不是个人资料页面:
mysite.com?page=2而不是mysite.com/profiles?page=2 -
尝试调整参数:
str_replace('/?', '/profiles?', $profiles->render());但是你不应该像这样破解它,其他地方肯定存在潜在问题......
标签: php laravel-5 laravel-pagination