3
votes

I'm having an error

Call to undefined method Illuminate\Database\Eloquent\Collection::Paginate()

I've been doing this:

public function index ()
{

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

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

2 Answers

6
votes

Try changing

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

to

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

By calling ->get(), you'd be getting a Collection object back, and there is no paginate() method in the Collection object, hence the error.

0
votes
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'));
 }