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.
Use this code in your blade,
{!! str_replace('/?', '?', $articles->render()) !!}
This code generate the correct url.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With