Laravel 5 Pagination linking to wrong place
Asked Answered
A

2

5

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!

Adventure answered 31/3, 2015 at 19:9 Comment(6)
mysite.com\?page=2 is the forward slash here correct or is it a typo?Syncarpous
Oops, that was a typo, i mean /Adventure
I wonder if a str_replace might work for you here, eg: str_replace('/?', '?', $profiles->render());Syncarpous
This will still link to the home page instead of the profiles page: mysite.com?page=2 instead of mysite.com/profiles?page=2Adventure
try adjust the args: str_replace('/?', '/profiles?', $profiles->render()); however you shouldnt need to hack it like this, there must be an underlying problem somewhere else...Syncarpous
I finally fixed this, thanks for your help. I do believe this is a bug in laravel 5.Adventure
A
14

I finally fixed this after hours of work!

I found out that you can pass the path to the paginator. As you can see this can be done by passing an array as the 5th argument when building an paginator object. This array will overwrite any options you pass it. We need to overwrite the path option. I'm getting the current path using Paginator::resolveCurrentPath().

So my code now looks like:

// Create a paginator instance.
$profiles = new Paginator($collection->all(), $total, $perPage, Paginator::resolveCurrentPage(), [
    'path' => Paginator::resolveCurrentPath()
]);

For people that are interested, the paginator constructor looks like:

__construct($items, $total, $perPage, $currentPage = null, array $options = [])

It is weird that you need to pass this manually and i would consider this a bug.

Adventure answered 31/3, 2015 at 20:18 Comment(0)
P
3

I won't complain if that's work for you. We have different approach on ORM and model loading, but I hope this will give you some insight.

Initial URL

localhost/application-name/public/publishers/?page=1

Controller

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]);
    }

View

<div class="row">
{!! $publishers->render() !!}
</div>

Resulting URL

localhost/application-name/public/publishers?page=1

I believe different server configuration and approach require different method. I, for myself, my development server are polluted with projects only separated by folder. So, this solution will suffice.

Once, I've read that you can solve this by setting DocumentRoot to Laravel public folder. But, it will be deemed unnecessary to configure virtual host for every development environment.

Postprandial answered 5/6, 2015 at 4:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.