我在 Laravel 5 中尝试使用分页器时遇到了一个奇怪的问题。
数据和分页信息已准备好,但是当我在 Blade 中调用 $model->render() 时,页面链接完全错误。
这是 Controller 中的一些示例代码:
public function index()
{
$articles = Article::latest('published_at')->paginate(3);
return view('articles/index')->with('articles',$articles);
}
Blade 中的代码:
{!! $articles->render() !!}
最后是路由中的代码:
Route::get('articles',array('as' => 'article-list','uses' => 'ArticleController@index'));
问题是 Laravel 为不同的页面生成了错误的 url,例如: example.com/articles/?page=2 , 在 ? 之前加上/。
有一种解决方法可以通过在将数据传递给 View 之前调用 setPath() 来更正 url,并且链接现在可以工作,如下所示:
$articles = Article::latest('published_at')->paginate(3);
$articles->setPath('articles');
return view('articles/index')->with('articles',$articles);
但是还有其他选项可以在 Laravel 5 中生成正确的页面链接而我错过了什么吗?
谢谢你。
环境更新:xampp。
最佳答案
在您的 Blade 中使用此代码,
{!! str_replace('/?', '?', $articles->render()) !!}
此代码生成正确的网址。
关于laravel - Laravel 5 分页生成链接的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28424522/