Sharif Ahmed
Sharif Ahmed

Reputation: 160

Laravel Pagination method is getting only the specfic number of row from db,not getting all the data

I am trying to add a pagination in my data table.but when I use pagination(25) ,it only bring 25 rows from the db and show total number to entity is 25. there is no 2|3|4|Next buttons to go to the next data.It does not bring all the data from the db and does not paginate them.

blade:

<table id="example1" class="table table-bordered table-hover">
    <thead>
    <tr>
        <th>SL</th>
        <th>Date</th>
        <th>Title</th>
    </tr>
    </thead>
    <tbody>
    <?php
    $i = 0;
    foreach($leads_list as $data){
    $i++;
    ?>
        <tr>
            <td><?php echo $i;?></td>
            <td>
                {{$data->created_at}}
            </td>
            <td>
                {{$data->title}}
            </td>
    
        </tr>
    <?php } ?>

    </tbody>
</table>

javascript:

$(function () {
    $("#example1").DataTable({
        "pageLength": 100,
        "responsive": true,
        "autoWidth": false,
    });
});

controller:

$leads_list = DB::table('leads')
    ->paginate(15);

return view("leads.index", compact('leads_list'));

want to get all the data from db and show them in table with 15data in each page. but it only bringing 15 rows from db,and there is no pagination.

but if i use ->get(); it works fine.but page gets slow.

Upvotes: 0

Views: 808

Answers (2)

Aless55
Aless55

Reputation: 2709

Please take a look at the documentation.

https://laravel.com/docs/7.x/pagination#displaying-pagination-results.

You will have to add the pagination links to your blade file. {{$leads_list->links()}}, this creates 1,2,3 etc. page links, so that you can switch between the paginated pages.

If you aren't using blade files, you can add following code to your view:

<?php echo $leads_list->render(); ?>

Upvotes: 1

Chris
Chris

Reputation: 1027

Firstly with a blade template there is no need to use <?php ?> tags, and to answer your question you have to display the pagination links:-

Your new blade file:-

<table id="example1" class="table table-bordered table-hover">
    <thead>
        <tr>
            <th>SL</th>
            <th>Date</th>
            <th>Title</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($leads_list as $list_item)
        <tr>
            <td>{{ $loop->index }}</td>
            <td>{{ $list_item->created_at }}</td>
            <td>{{ $list_item->title }}</td> 
        </tr>
        @endforeach
    </tbody>
</table>

{{ $leads_list->links() }}

Upvotes: 2

Related Questions