簡體   English   中英

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

[英]Laravel 5 - Call to undefined method Illuminate\Database\Eloquent\Collection::Paginate()

我有一個錯誤

調用未定義的方法 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'));
}

嘗試改變

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

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

通過調用->get() ,你會得到一個Collection對象返回,並沒有paginate()方法的Collection對象,因此錯誤。

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

請從查詢中刪除 get()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM