Pagination with filter - Laravel

Asked

Viewed 191 times

-1

I’m having trouble paging using filters. Simple paging works normally, but when filter and advance to a next page, I lose filters.

I had never used filters, if it is not running in the best way, I am open to suggestions.

The filter can be made by category and/or description and/or reference.

Controller:

$ferramentas = Ferramenta::when(Request::input('categoria'),function($query){
                        $query->where('categoria',Request::input('categoria'));
                    })
                    ->when(Request::input('descricao'),function($query){
                        $query->where('description',  'like', '%' . strtoupper(Request::input('descricao')) . '%');
                    })
                    ->when(Request::input('referencia'),function($query){
                        $query->where('item','like', '%' . strtoupper(Request::input('referencia')) . '%');
                    })->paginate(10);

    $categoria = Request::input('categoria');
    $descricao = Request::input('descricao');
    $referencia = Request::input('referencia');               

    return view('ferramentas', compact('ferramentas', 'categoria', 'descricao', 'referencia'));

View:

<div class="container-fluid">
        <!-- Page Heading -->
        <div class="card shadow mb-4">
            <div class="card-header py-3">
              <h6 class="m-0 font-weight-bold text-primary">Filtro</h6>
            </div>
            <div class="card-body">
              <form action="/ferramentas" method="POST">
                @csrf
                <div class="row">

                    <div class="col-md-2 px-1">
                        <label for="referencia">Referência</label>
                        @if ($referencia)
                            <input type="text" class="form-control" placeholder="" value="{{$referencia}}" name="referencia">
                        @else
                            <input type="text" class="form-control" placeholder="" name="referencia">
                        @endif
                    </div>

                    <div class="col-md-4 px-1">
                        <label for="descricao">Descrição</label>
                        @if ($descricao)
                            <input type="text" class="form-control" value="{{$descricao}}" name="descricao" >
                        @else
                            <input type="text" class="form-control" value="" name="descricao" >
                        @endif
                    </div>

                    <div class="col-md-4 px-1">
                        <label for="categoria">Categoria</label>
                        <select class="form-control" name="categoria">
                            <option value="" selected disabled>Selecione uma categoria</option>
                            @if ($categoria)
                                <option value="{{$categoria}}" selected>{{$categoria}}</option>
                            @endif
                            @foreach ($categorias as $cat)
                                <option value="{{$cat->categoria}}">{{$cat->categoria}}</option>
                            @endforeach
                        </select>
                    </div>

                    <div class="col-md-1  align-self-end px-1 button">
                      <button type="submit" class="btn btn-secondary"><i class="fa fa-search fa-1x"></i></button>
                    </div>
                    @if ($referencia || $descricao || $categoria )
                        <div class="col-md-1  align-self-end px-1 button">
                            <a href="ferramentas" class="text-danger">Limpar filtro</a></button>
                        </div>
                    @endif

                </div>
              </form>

            </div>
        </div>



        <!-- Listagem -->
        <div class="card shadow mb-4">
          <div class="card-header py-3">
            <h6 class="m-0 font-weight-bold text-primary">Ferramentas</h6>
          </div>
          <div class="card-body">
            <div class="table-responsive">
              <table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
                <thead>
                  <tr>
                    <th>Item</th>
                    <th>Descrição</th>
                    <th>Categoria</th>
                    <th>Part Number - Fabricante</th>
                    <th>Ações</th>
                  </tr>
                </thead>

                <tbody>
                  @foreach ($ferramentas as $frmt)
                    <tr>
                      <td>{{$frmt->item}}</td>
                      <td>{{$frmt->description}}</td>
                      <td>{{$frmt->categoria}}</td>
                      <td>{{$frmt->pn_fabricante}}</td>
                      <td style="width:171px">
                          <a class="btn btn-primary btn-sm" href="/ferramentas/{{$frmt->inventory_item_id}}">Detalhes</a>

                          <button type="button" class="btn btn-secondary btn-sm" data-toggle="modal" data-target="#modalSolicitacao" onclick="setaDadosModal('{{$frmt->inventory_item_id}}', '{{$frmt->description}}' , '{{$frmt->pn_fabricante}}')" >
                            Solicitar
                          </button>
                      </td>
                    </tr>

                  @endforeach
                </tbody>
              </table>
              <div class="d-flex justify-content-center"> {{$ferramentas->links()}} </div>

            </div>
          </div>
        </div>

    </div>

1 answer

0


I’m sorry, you guys! Shortly after publishing, I realized what the problem is.

Just for the record:

First use get instead of pos in the search form;

<div class="card-body">
              <form action="/ferramentas" method="get">
                <div class="row">

2nd append method on pagination link with the search fields.

<div class="d-flex justify-content-center"> {{ $ferramentas->appends(['descricao' => $descricao, 'referencia' => $referencia, 'categoria' => $categoria ])->links()}} </div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.