"Method links does not exist" with Laravel pagination
Asked Answered
T

1

3

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>
Transpontine answered 13/11, 2016 at 15:9 Comment(0)
C
15

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().

Caterpillar answered 13/11, 2016 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.