首页 文章

Laravel 5 Paginator生成的链接问题

提问于
浏览
5

当我尝试在Laravel 5中使用Paginator时,我遇到了一个奇怪的问题 . 数据和分页信息已经准备好了,但是当我在刀片中调用$ model-> render()时,页面链接完全错误 .

以下是控制器中的一些示例代码:

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

和刀片中的代码:

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

最后是路线中的代码:

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

问题是Laravel会为不同的页面生成错误的URL: example.com/articles/?page=2 ,还有其他/之前的?

有一种解决方法可以在将数据传递给视图之前通过调用setPath()来更正URL,现在链接可以正常工作,如下所示:

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

但是还有其他选项来生成Laravel 5中页面的正确链接吗?我错过了什么?

谢谢 .


环境更新:xampp .

1 回答

  • 10

    在刀片中使用此代码,

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

    此代码生成正确的URL .

相关问题