4
votes

Here is my route:

Route::namespace('Admin')->prefix('admin')->name('admin.')->middleware(['auth', 'auth.admin'])->group(function () {

Route::prefix('users')->group(function() {
    Route::get('/', 'UserController@index')->name('user');

In my controller I have the following:

$users = User::with('roles', 'group')->paginate(1);

return view('admin.user.index')->with(['users' => $users]);

Without the pagination, this works fine and returns the 4 results I have. with the pagination the UI part of things work, it displays me 4 links in the pagination controls (1 result per page) but when clicking the link to page 2, 3 and 4 it just returns the same first item every time.

I can see it's passing the URL param in the network tab for example:

http://localhost:8050/admin/users?page=2

However, it has no effect on the result returned so I assume the issue is with my Eloquent query?


After some more research, the problem isn't with the pagination the issue is with the GET params.

Even when I pass the page key directly in the URL like so: http://localhost:8050/admin/users?page=2 my GET params are empty so that's why the pagination isn't working but whats blocking the GET params from getting through?

Even doing a dd($_GET) shows the GET params as being empty.

3

3 Answers

1
votes

Use this:

$paginate = $request->input('page');
$itemsPerPage = 10;

if(!$paginate){
   $paginate = 1;
}   

$users = User::with('roles', 'group')->skip($paginate*$itemsPerPage)->take($itemsPerPage);

return view('admin.user.index')->with(['users' => $users]);
-1
votes

try this code

In Controller users( change with your table name)

$users = DB::table('users')->paginate(1);

return view('user.index', ['users' => $users]);

In View

{{ $users->links() }}
-1
votes

In view file you can change this:

{!! $users->links() !!}