Md Mahfuzur Rahman
Md Mahfuzur Rahman

Reputation: 2359

Pagination is not working properly in Laravel 5.2

I am working on a project built with Laravel 5.2.*. I have a table named as users (associated with roles table) and model User for the table. I want to show the users list with pagination. My codes are given below.

In Controller:

public function index()
{
    $page_title = "Manage User";

    $roles = Role::where('status', 1)->get();

    $users = User::where('status', 1)->paginate(10);

    return view('users.index', compact('page_title', 'users', 'roles'));
}

The data format of users variable is like below.

LengthAwarePaginator {#199 ▼
  #total: 3
  #lastPage: 1
  #items: Collection {#202 ▼
    #items: array:3 [▼
      0 => User {#203 ▶}
      1 => User {#204 ▶}
      2 => User {#205 ▶}
    ]
  }
  #perPage: 10
  #currentPage: 1
  #path: "http://localhost/dtrdimen/UsersList"
  #query: []
  #fragment: null
  #pageName: "page"
}

In View file:

    <table class="table table-bordered table-hover">
        <tr>
            <th style="text-align: center;" width="10%">Id</th>
            <th style="text-align: center;" width="30%">Name</th>
            <th style="text-align: center;" width="15%">Email</th>
            <th style="text-align: center;" width="15%">Role</th>
            <th style="text-align: center;" width="10%">Status</th>
            <th style="text-align: center;" width="20%" colspan="3">Actions</th>
        </tr>

        @foreach ($users as $user)
            <tr>
                <td>{{ $user->id }}</td>
                <td>{{ $user->name }}</td>
                <td>{{ $user->email }}</td>
                <td>{{ $user->role_id }}</td>
                <td>{{ $user->status }}</td>

                <td style="">

                    <a href="/editUser/{{ $user->id }}" class="btn btn-warning pull-left", style="margin-right: 5px;">Edit</a>

                    {!! Form::open(['method' => 'delete', 'route' => ['User.remove', $user->id], 'class' => 'pull-left']) !!}
                    {!! Form::submit('Delete', ['class' => 'btn btn-danger', 'onClick' => "return check()"]) !!}
                    {!! Form::close() !!}
                </td>
            </tr>
        @endforeach
    </table>
    <div class="text-center">
        {!! $users->links() !!} //I also used render() instead of links
    </div>

I don't know what I have missed. This code is not showing the pagination. Can anyone help me to fix the issue here ?

Upvotes: 0

Views: 2246

Answers (2)

S M Abrar Jahin
S M Abrar Jahin

Reputation: 14588

U should have done something like this-

Controller-

public function paging() {
    $users = User::paginate(10);
    return View::make('paging', compact('users'));
}

View-

@extends('layout.default')
@section('content')
<div class="container">
  <table width="60%" cellspacing="0" cellpadding="4" border="0" class="data">
    @foreach($users as $user)
    <tr>
      <td> {{ $user->u_firstname }} </td>
      <td> {{ $user->u_lastname }} </td>
      <td> {{ $user->u_email }} </td>
    </tr>
    @endforeach
  </table>
  <div class="pagination"> {{ $users->links() }} </div>
</div>
@stop

And Route-

Route::get('account/paging', 'AccountController@paging');

Tutorials on creating pagination is given -

http://learn24bd.com/laravel-5-tutorial-06-pagination-in-laravel-5/

And

http://tutsnare.com/how-to-create-pagination-in-laravel/

And documentation is given here-

https://laravel.com/docs/5.1/pagination

Upvotes: 0

Mihai Matei
Mihai Matei

Reputation: 24276

There is no link to be displayed as long as you have less items than your selection.

To test it just use:

$users = User::where('status', 1)->paginate(1);

Upvotes: 2

Related Questions