Hao Zhou's avatar

Problems of links generated by Laravel 5 Paginator

I came across a weird problem when I tried to use Paginator in Laravel 5. The data and pagination information were prepared, but when I called $model->render() in blade the links to pages were simply wrong.

Here is some sample code in controller:

    public function index()
    {
        $articles = Article::latest('published_at')->paginate(3);
        return view('articles/index')->with('articles',$articles);
    }

And the code in blade:

    {!! $articles->render() !!}

Lastly the code in routes:

    Route::get('articles',array('as' => 'article-list','uses' => 'ArticleController@index'));

The problem is Laravel generates wrong urls to different pages as such : example.com/articles/?page=2, with additional / before ?.

There is a workaround to correct the url by calling setPath() before passing data to view, and links now work, like this:

    $articles = Article::latest('published_at')->paginate(3);
    $articles->setPath('articles');
    return view('articles/index')->with('articles',$articles);

But are there other options to generate correct links to pages in Laravel 5 and I missed something?

Thank you.

0 likes
6 replies
Hao Zhou's avatar

Update on the environment : xampp.

Anyone encountered similar problems and figured what the cause was?

pmall's avatar

example.com/articles/?page=2 is a perfectly fine link. Is this the / that bother you ?

Hao Zhou's avatar

Hi @pmall, the trailing / does matter on xampp, it simply messes up all page links.

I'll have a try on another environment like homestead.

pmall's avatar

This link is fine and your server should handle this correctly. I don't think it is a bug.

Hao Zhou's avatar

But Apache in xampp cannot handle the URLs, that's why I started to post this thread.

Please or to participate in this conversation.