Laravel 5 - 调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: Paginate()

时间:2016-06-18 10:43:51

标签: php laravel-5 pagination

我有错误

  

调用未定义的方法Illuminate \ Database \ Eloquent \ Collection :: Paginate()

我一直这样做:

public function index ()
{

    $articles = Article::latest('published_at')->published()->get()->paginate(5);
    $articlesLink = $articles->render();

    return view('articles.index', compact('articles', 'articlesLink'));
}

2 个答案:

答案 0 :(得分:4)

尝试更改

$articles = Article::latest('published_at')->published()->get()->paginate(5);

$articles = Article::latest('published_at')->published()->paginate(5);

通过调用->get(),您将获得一个Collection对象,并且paginate()对象中没有Collection方法,因此出错。

答案 1 :(得分:0)

public function index () {

  // you =>  $articles = Article::latest('published_at')->published()->get()->paginate(5);
 // me => $articles = Article::latest('published_at')->latest()->paginate(5);
    // Unnecessary  $articlesLink = $articles->render();

    return view('articles.index', compact('articles'));
 }