phplaravellaravel-pagination

"Method links does not exist" with Laravel pagination


I'm passing a collection of posts to my view and I'm trying to use pagination, but I have this error:

Method Illuminate\Database\Eloquent\Collection::links does not exist

CONTROLLER

public function index() {
    $posts = Post::where('visible', 1)
        ->where('expire_date', '>', $current)
        ->where('delete', 0)
        ->get();

    return view('search', compact('posts'));
}

VIEW

<div class="pagination-bar text-center">
       {{ $posts->links() }}
</div>

Solution

  • Change your code to this:

    $posts = Post::where('visible', 1)
                 ->where('expire_date', '>', $current)
                 ->where('delete', 0)
                 ->paginate(1);
    
    return view('search', compact('posts'));
    

    Your code doesn't work because you need to call paginate() on the results instead of get() or all().