0
votes

I am trying to paginate my results from a database using a Laravel 5.2.31 app, but the pagination isn't working.

When I use {!! $users->links() !!} in my view, I get an error 'Method links does not exist' .

The same thing happens when I use {!! $users->render() !!}

My Controller:

public function index() {
    $users= User::orderBy('created_at', 'desc')->paginate(4); 
    return view('home', ['users' => $users]); 
}

Any help?

1
View the contents of $users by doing dd($users); before calling the view.linuxartisan
as linuxartisan said dump the object and see if thats of type LengthAwarePaginatorCerlin
Yes, it is of type LengthAwarePaginator. How can I render links?user3708740
Ok. Things worked. This is how : public function index(Request $request){ $users= User::orderBy('created_at', 'desc')->paginate(4); $page = $request->get('page', 1); $perPage = 4; $offset = ($page * $perPage) - $perPage; $usersarray = (array)$users; $paginator = new LengthAwarePaginator(array_slice($usersarray, $offset, $perPage, true), count($usersarray), $perPage, $page, ['path' => $request->url(), 'query' => $request->query()]); return view('home', ['users' => $users, 'paginator' => $paginator]); }user3708740

1 Answers

0
votes

Could you please try this:
return \View::make('home')->with('users',$users);