3
votes

i'm passing my array $posts to my view and i'm tryng use pagination but i have the error:

Method links does not exist. (View: C:\xampp\htdocs\app\resources\views\search.blade.php)

CONTROLLER

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

VIEW

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

1 Answers

14
votes

Change you 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 do not save paginate() results to a variable, like $posts = $posts->paginate(1);. Also, you shouldn't use get() or all() after paginate().