mayuri0606's avatar

Why pagination shows all the content on every page?

I have added pagination as follows. But all the pages display the same content. Think that there are 8 records in the employee table. When I added the below code to my laravel project, on the first page it displays all 8 records and on the second page also it displays the same 8 records.

It doesn't break the first 5 records to the first page and last 3 records to the second page.

controller

    public function all()
    {
        $data = DB::table('employees')->paginate(5);
        $employeeshow = employee::all()->toArray();
        return view('employee.index', compact('employeeshow', 'data'));
    }

index.blade.php

<div>
 <table>
     <tr>
       <th>First Name</th>
       <th>Last Name</th>
     </tr>
   @foreach($employeeshow as $row)
     <tr>
       <td>{{$row['firstname']}}</td>
       <td>{{$row['lastname']}}</td>
     </tr>
   @endforeach
 </table>
  {{ $data->links() }}
</div>

I want to display first 5 records on first page, next 5 records in the second page likewise. Thank you

0 likes
6 replies
frankielee's avatar

Does the number at end of URL change?

http://domain.com/employess/?page=2  <=
MichalOravec's avatar

What do you mean same data?

@foreach ($data as $employee)
    {{ $employee->id }}
@endforeach

{{ $data->links() }}
mayuri0606's avatar

@michaloravec

What I wanted to say is that, think there are 8 records in the employee table. When I added the above code to my laravel project, on the first page it displays all 8 records and on the second page also displays the same 8 records.

It doesn't break the first 5 records to the first page and last 3 records to the second page. I want to display the first 5 records on the first page, next 5 records on the second page likewise.

Snapey's avatar

How are you iterating over the employees in the view?

mayuri0606's avatar

@frankielee - yes

@MichalOravec & @Snapey -

controller

    public function all()
    {
        $data = DB::table('employees')->paginate(5);
        $employeeshow = employee::all()->toArray();
        return view('employee.index', compact('employeeshow', 'data'));
    }

I have added my view.blade as follows.

<div>
 <table>
     <tr>
       <th>First Name</th>
       <th>Last Name</th>
     </tr>
   @foreach($employeeshow as $row)
     <tr>
       <td>{{$row['firstname']}}</td>
       <td>{{$row['lastname']}}</td>
     </tr>
   @endforeach
 </table>
  {{ $data->links() }}
</div>

May I know where I how can I do it? It means should I write another foreach?

MichalOravec's avatar
Level 75

@mayuri0606 Change it to this

public function all()
{
    $employees = employee::paginate(5);
    
    return view('employee.index', compact('employees'));
}
<div>
    <table>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
        </tr>

        @foreach($employees as $employee)
            <tr>
                <td>{{ $employee->firstname }}</td>
                <td>{{ $employee->lastname }}</td>
            </tr>
        @endforeach
    </table>

    {{ $employees->links() }}
</div>

And why do you have you model as employee lowercase?

Please or to participate in this conversation.