Laravel 5 分页链接到错误的地方
Laravel 5 Pagination linking to wrong place
我目前正在将我的一个项目从 4.2 更新到 Laravel 5。我知道分页器 class 发生了很多变化,但我真的不明白为什么这不起作用。我在我项目的多个位置的 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()
link 到我项目的根目录后生成的 links 而不是当前页面。
示例:
link 位于 mysite.com/profiles
但 link 到 mysite.com/?page=2
而不是 mysite.com/profiles?page=2
我的代码在 Laravel 4.2 中运行良好,IL link 下面供参考:
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;
欢迎任何帮助。
谢谢!
经过几个小时的工作,我终于解决了这个问题!
我发现您可以将路径传递给分页器。
如您所见,这可以通过在构建分页器对象时将数组作为第 5 个参数传递来完成。该数组将覆盖您传递给它的任何选项。我们需要覆盖 path
选项。我正在使用 Paginator::resolveCurrentPath()
获取当前路径。
所以我的代码现在看起来像:
// Create a paginator instance.
$profiles = new Paginator($collection->all(), $total, $perPage, Paginator::resolveCurrentPage(), [
'path' => Paginator::resolveCurrentPath()
]);
对于感兴趣的人,分页器构造函数如下所示:
__construct($items, $total, $perPage, $currentPage = null, array $options = [])
你需要手动传递这个很奇怪,我认为这是一个错误。
如果这对您有用,我不会抱怨。我们在 ORM 和模型加载方面有不同的方法,但我希望这能给你一些见解。
初始URL
localhost/application-name/public/publishers/?page=1
控制器
class PublisherController extends Controller {
public function index()
{
$publ = Publisher::paginate(5);
$publ->setPath(''); //I just use custom Url and set path to ''
return view('publisher-table', ['publishers'=>$publ]);
}
查看
<div class="row">
{!! $publishers->render() !!}
</div>
结果URL
localhost/application-name/public/publishers?page=1
我相信不同的服务器配置和方法需要不同的方法。我,就我自己而言,我的开发服务器被仅由文件夹分隔的项目污染。所以,这个解决方案就足够了。
有一次,我了解到您可以通过将 DocumentRoot
设置为 Laravel public
文件夹来解决此问题。但是,将认为没有必要为每个开发环境配置虚拟主机。
我目前正在将我的一个项目从 4.2 更新到 Laravel 5。我知道分页器 class 发生了很多变化,但我真的不明白为什么这不起作用。我在我项目的多个位置的 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()
link 到我项目的根目录后生成的 links 而不是当前页面。
示例:
link 位于 mysite.com/profiles
但 link 到 mysite.com/?page=2
而不是 mysite.com/profiles?page=2
我的代码在 Laravel 4.2 中运行良好,IL link 下面供参考:
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;
欢迎任何帮助。 谢谢!
经过几个小时的工作,我终于解决了这个问题!
我发现您可以将路径传递给分页器。
如您所见,这可以通过在构建分页器对象时将数组作为第 5 个参数传递来完成。该数组将覆盖您传递给它的任何选项。我们需要覆盖 path
选项。我正在使用 Paginator::resolveCurrentPath()
获取当前路径。
所以我的代码现在看起来像:
// Create a paginator instance.
$profiles = new Paginator($collection->all(), $total, $perPage, Paginator::resolveCurrentPage(), [
'path' => Paginator::resolveCurrentPath()
]);
对于感兴趣的人,分页器构造函数如下所示:
__construct($items, $total, $perPage, $currentPage = null, array $options = [])
你需要手动传递这个很奇怪,我认为这是一个错误。
如果这对您有用,我不会抱怨。我们在 ORM 和模型加载方面有不同的方法,但我希望这能给你一些见解。
初始URL
localhost/application-name/public/publishers/?page=1
控制器
class PublisherController extends Controller {
public function index()
{
$publ = Publisher::paginate(5);
$publ->setPath(''); //I just use custom Url and set path to ''
return view('publisher-table', ['publishers'=>$publ]);
}
查看
<div class="row">
{!! $publishers->render() !!}
</div>
结果URL
localhost/application-name/public/publishers?page=1
我相信不同的服务器配置和方法需要不同的方法。我,就我自己而言,我的开发服务器被仅由文件夹分隔的项目污染。所以,这个解决方案就足够了。
有一次,我了解到您可以通过将 DocumentRoot
设置为 Laravel public
文件夹来解决此问题。但是,将认为没有必要为每个开发环境配置虚拟主机。