【问题标题】:Laravel 5 - Call to undefined method Illuminate\Database\Eloquent\Collection::Paginate()Laravel 5 - 调用未定义的方法 Illuminate\Database\Eloquent\Collection::Paginate()
【发布时间】:2016-10-20 03:32:40
【问题描述】:
我遇到了错误
调用未定义的方法 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'));
}
【问题讨论】:
标签:
php
laravel-5
pagination
【解决方案1】:
尝试改变
$articles = Article::latest('published_at')->published()->get()->paginate(5);
到
$articles = Article::latest('published_at')->published()->paginate(5);
通过调用->get(),您将得到一个Collection 对象,而Collection 对象中没有paginate() 方法,因此会出现错误。
【解决方案2】:
公共函数索引(){
// 你 => $articles = Article::latest('published_at')->published()->get()->paginate(5);
// 我 => $articles = Article::latest('published_at')->latest()->paginate(5);
// 不必要的 $articlesLink = $articles->render();
返回视图('articles.index',紧凑('articles'));
}