Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.


Update on environment: xampp.

like image 938
Carter Avatar asked Feb 10 '15 05:02

Carter


1 Answers

Use this code in your blade,

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

This code generate the correct url.

like image 80
Vinod VT Avatar answered Sep 23 '22 07:09

Vinod VT