Problems of links generated by Laravel 5 Paginator

2019-02-22 13:20发布

问题:

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.


Update on environment: xampp.

回答1:

Use this code in your blade,

{!! str_replace('/?', '?', $articles->render()) !!}

This code generate the correct url.