I'm currently updating one of my projects to Laravel 5 from 4.2. I'm aware a lot has changed with the paginator class but i really can't figure out why this isn't working. I call paginate() on eloquent models at multiple locations in my project and every thing works great.
But this same project has a profile searching page with filters so i have to call a huge custom DB::table() query. After that i want to build a paginator object from the results.
$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;
My problem is that the links that are generated after calling $profiles->render()
link to the root of my project instead of the current page.
Example:
The links are located at mysite.com/profiles
but link to mysite.com/?page=2
instead of mysite.com/profiles?page=2
My code worked great in Laravel 4.2, il link it below for reference:
Laravel 4.2 code that works:
$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;
Any help is welcome. Thanks!
mysite.com\?page=2
is the forward slash here correct or is it a typo? – Syncarpousstr_replace
might work for you here, eg:str_replace('/?', '?', $profiles->render());
– Syncarpousmysite.com?page=2
instead ofmysite.com/profiles?page=2
– Adventurestr_replace('/?', '/profiles?', $profiles->render());
however you shouldnt need to hack it like this, there must be an underlying problem somewhere else... – Syncarpous