Laravel pagination with search filter? [duplicate]

1

I have a search filter on my home screen. When I search, POST the filter data and I can not get the pagination link redirected with the search filters.

Every time I have a filtered search and I click on any of the links this filter is lost.

Controller:

public function index()
{
    return $this->listar();
}

public function listar(Request $request = NULL) 
{

    if ( !empty( $request )) {
        $data = $request->all();
    } else {
        $data = NULL;
    }

    $validator = NULL;

    if ( empty( $data['busca'] ) ) {            

        $pacientes = Paciente::joinPessoa()->paginate(10);

    } 
    else 
    {

        if ($data['tipo_busca'] == 'nome') {

            $pacientes = DB::table('pacientes AS pac')
                        ->leftJoin('pessoas', 'pac.id_pessoa', '=', 'pessoas.id')
                        ->select('pessoas.nome AS nome','pessoas.dtnasc',  'pac.ficha_atendimento', 'pac.id')
                        ->where($data['tipo_busca'], 'ilike', $data['busca'] . '%')
                        ->orderby('nome')
                        ->paginate(10);

        } else {

            $validate = $this->validate($request, [
                'busca' => 'numeric',
            ]);

            $pacientes =  DB::table('pacientes AS pac')
                           ->leftJoin('pessoas', 'pac.id_pessoa', '=', 'pessoas.id')
                           ->select('pessoas.nome AS nome', 'pessoas.dtnasc', 'pac.ficha_atendimento', 'pac.id')
                           ->where('pac.'.$data['tipo_busca'], '=', $data['busca'])
                           ->orderby('nome')
                           ->paginate(10);

        }

    }

    $title = "Listagem de pacientes";
    $links = $pacientes->links();
    return view('paciente.list', compact('title', 'pacientes', 'links'))
        ->withErrors($validator);
}

View:

    <div class="container-fluid form">

      @if( isset($errors) && count( $errors ) > 0 )
    <div class='alert alert-danger'>
      @foreach( $errors->all() as $erro )

        <p>{{ $erro }}</p>

      @endforeach
    </div>
  @endif
        <div class='row'>
            <div class="col-md-10">
                <form method='post' action="{{ route('paciente.listar') }}" class='form-horizontal'>
                    {!! csrf_field() !!}
                    <div class="col-md-5">
                        <input type="text" class="form-control" id="busca" name='busca' placeholder="Busca" value="{{ old('busca') }}" />
                    </div>
                    <label for="nome" class="col-sm-2 control-label">Buscar por:</label>
                    <div class="col-sm-3">
                    <select class='form-control' name='tipo_busca'>
                        <option value='nome'>Nome</option>
                        <option value="ficha_atendimento">Ficha Atendimento</option>
                        <option value="id">Código</option>
                    </select>
                </div>
                    <div class="col-md-2">
                        <button type='submit' class='btn btn-primary pull-right'>
                        <i class='glyphicon glyphicon-search'></i>
                            Buscar
                        </button>
                    </div>
                </form>
            </div>
            <div class="col-md-2">
                <a href="{{ route('paciente.create') }}" class='btn btn-default btn-add'>
                    <i class='glyphicon glyphicon-plus'></i>
                    Novo
                </a>
            </div>
        </div>
    <div class="row">
        <div class="col-md-12">
                <table class='table table-striped'>
                    <thead>
                        <tr>
                            <th>Código</th>
                            <th>Nome</th>
                            <th>Data Nascimento</th>
                            <th>Ficha de Atendimento</th>
                            <th width='100px'>Ações</th>
                        </tr>
                    </thead>
                    @forelse($pacientes as $paciente)
                        <tr>
                            <td>{{ $paciente->id }}</td>
                            <td>{{ $paciente->nome }}</td>
                            <td>{{ \Carbon\Carbon::parse( $paciente->dtnasc )->format( 'd/m/Y' ) }}</td>
                            <td>{{ $paciente->ficha_atendimento }}</td>
                            <td>
                                <a href="{{route( "paciente.edit", $paciente->id )}}" class='actions edit'>
                                    <span class='glyphicon glyphicon-pencil'></span>
                                </a>
                                <a href='#' class='actions delete'>
                                    <span class='glyphicon glyphicon-trash'></span>
                                </a>
                            </td>
                        </tr>               
                    @empty
                        <tr>
                            <td colspan="4">
                                Nenhum paciente cadastrado
                            </td>
                        </tr>
                    @endforelse 
                    <tfoot>
                        <tr>
                            <td colspan="5" class='text-center'>
                                {{ $links }}
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    
asked by anonymous 02.06.2017 / 15:42

1 answer

0

Use the appends method to add filters in the% of pages%

$links = $pacientes
    ->appends(['tipo_busca' => $data['tipo_busca'], 'busca' => $data['busca'] ])
    ->links();

02.06.2017 / 16:46