AquinoBR's avatar

How to condition pagination

Below is the one page code that lists some data, however I made a condition to only sample one type in that listing, but in the page it counts all the data in the table in mysql. I want to know how the pagination shows only the data for this condition on that page.


@section('content')
<div class="content">
        @include('admin.includes.alerts')
          <div class="row">
            <div class="col-lg-12 col-xlg-12 col-md-12">
              <div class="card">
                  <ul class="nav nav-tabs profile-tab" role="tablist">
                      <li class="nav-item"> <a class="nav-link" href="htrade">Histórico de Troca</a> </li>
                      <li class="nav-item"> <a class="nav-link active" href="hdeposit">Histórico de Depósito</a> </li>
                      <li class="nav-item"> <a class="nav-link" href="hwithdraw">Histórico de Retirada</a> </li>
                  </ul>
                  <div class="tab-content">
                      <div class="tab-pane active" id="hdeposito" role="tabpanel">
                          <div class="card-block">
                            <div class="card">
                              <div class="card-header">
                                <div class="float-left"><h5 class="title">Histórico de Depósito</h5></div>
                                <div class="float-right">
                                  <form action="{{ route('hdeposit.search') }}" method="POST">
                                    {!! csrf_field() !!}
                                    <div class="input-group" style="height: 30px;">
                                      <input type="text" name="id" class="form-control" placeholder="PESQUISA POR ID">
                                      <span class="input-group-addon">
                                        <button type="submit" class="btn btn-primary"><i class="fas fa-search"></i></button>
                                      </span> 
                                      
                                    </div>
                                  </form>
                                </div>
                              </div>
                              <div class="card-body">
                                <div class="table-responsive">
                                  <table class="table table-striped tablefixed">
                                    <thead class="text-primary">
                                      <th><strong>ID</strong></th>
                                      <th><strong>Moeda</strong></th>
                                      <th><strong>Quantidade</strong></th>
                                      <th><strong>Saldo Antes</strong></th>
                                      <th><strong>Saldo Depois</strong></th>
                                      <th><strong>Status</strong></th>
                                      <th><strong>Comprovar</strong></th>
                                      <th><strong>Data</strong></th>
                                    </thead>
                                    <tbody>
                                      @forelse($historics as $historic)
                                        @if ($historic->type != 'I')
                                          @continue
                                        @endif
                                      <tr>
                                        <td>{{ $historic->id }}</td>
                                        <td>{{ $historic->name }}</td>
                                        <td>{{ number_format($historic->amount, 8, '.', ',') }}</td>
                                        <td>{{ number_format($historic->total_before, 8, '.', ',') }}</td>
                                        <td>{{ number_format($historic->total_after, 8, '.', ',') }}</td>
                                        <td>{{ $historic->status }}</td>
                                        <td>
                                          @if ($historic->status == 'Aguardando Depósito')
                                            <a class="btn btn-primary btn-sm" href="{{ route('deposit.brl.confirm') }}">Enviar Comprovante</a>
                                          @else
                                            <div class="text-center">-</div>
                                          @endif
                                        </td>
                                        <td>{{ $historic->date }}</td>
                                      </tr>
                                      @empty
                                      @endforelse
                                    </tbody>
                                  </table>
                                </div>
                                <div class="row">
                                  <div class="col-md-6">
                                <nav aria-label="Page navigation example">
                                  <ul class="pagination justify-content">
                                    <li class="page-item"><a class="page-link" href="#"><i class="fas fa-file-alt"></i></a></li>
                                    <li class="page-item"><a class="page-link" href="#"><i class="fas fa-file-excel"></i></a></li>
                                    <li class="page-item"><a class="page-link" href="#"><i class="fas fa-file-pdf"></i></a></li>
                                    <li class="page-item"><a class="page-link" href="#"><i class="fas fa-copy"></i></a></li>
                                    <li class="page-item"><a class="page-link" href="#"><i class="fas fa-print"></i></a></li>
                                  </ul>
                                </nav>
                                </div>
                                <div class="col-md-6">
                                <nav aria-label="Page navigation example">
                                  <ul class="pagination justify-content-end">
                                    <li>
                                      {!! $historics->links() !!}
                                    </li>
                                  </ul>
                                </nav>
                                </div>
                              </div>
                              </div>
                            </div>
                          </div>
                      </div>
                  </div>
              </div>
            </div>
          </div>
      </div>
@endsection
0 likes
2 replies
lostdreamer_nl's avatar
Level 53

In the controller, where you query for the data you should have set the where clause to only get the data you want instead of querying for ALL data and then filtering it out inside your loop.

You have something like:

$historics = History::get()->paginate();

Which should be changed to something like:

$historics = History::where('type', 'I')->get()->paginate();
2 likes

Please or to participate in this conversation.