pag66's avatar
Level 5

Pagination and filter

Hello everyone, I have a filter for my users, based on some criterias, but when I choose the second filter page it doesn't display anything, what I'm doing wrong ? Thanks for the help

Here is my Controller filter function.

 public function filter(Request $request){
            $clinica = Clinica::first();

         // filtrado por valor apellido
        if ($request->has('apellidos')){
            $pacientes= Paciente::where('apellidos_pac','LIKE','%'.$request->apellidos.'%') ->orderBy('apellidos_pac','asc')->paginate(15);
        }
          if ($request->has('cedula')){
         // filtrado por valor cedula 
            $pacientes= Paciente::where('cedula_pac','LIKE','%'.$request->cedula.'%') ->orderBy('apellidos_pac','asc')->paginate(15);
        } 
          if ($request->has('historia')){
         // filtrado por valor historia
            $pacientes= Paciente::where('historia','LIKE','%'.$request->historia.'%') ->orderBy('apellidos_pac','asc')->paginate(15);
        } 
         if ($request->has('ciudad')){ 
         // filtrado por valor ciudad
            $pacientes= Paciente::where('lugresid_pac','LIKE','%'.$request->ciudad.'%') ->orderBy('apellidos_pac','asc')->paginate(15);
        } 
         if ($request->has('otros_pac')){ 
          // filtrado por valor otros_pac
            $pacientes= Paciente::where('otros_pac','LIKE','%'.$request->otros_pac.'%') ->orderBy('apellidos_pac','asc')->paginate(15);
          
        }
        return view ('pacientes.filter',compact('pacientes','clinica'));
        
    } 

My view is this

@extends('layout.layout')
@section('content')            
<div class="row">
                <div class="col-sm-3 mol-md-2 sidebar">
                @can('ver_paciente')
                 <h3>Filtrar contenido</h3>
                            {{ Form::open(array('url' => 'paciente/filter')) }}
                                {{-- Apellidos --}}
                                <fieldset class="form-group">
                                    <label class="col-sm-2 control-label" for="apellidos">Apellidos</label>
                                    <input class="form-control" type="text" id="apellidos" name="apellidos">
                                </fieldset>
                                {{-- Cedula --}}
                                <fieldset class="form-group">
                                    <label class="col-sm-2 control-label" for="cedula">Cédula</label>
                                    <input class="form-control" type="text" id="cedula" name="cedula">
                                </fieldset>
                                {{-- Historia --}}
                                <fieldset class="form-group">
                                    <label class="col-sm-2 control-label" for="historia">Historia clínica</label>
                                    <input class="form-control" type="text" id="historia" name="historia">
                                </fieldset>
                                {{-- Ciudad --}}
                                <fieldset class="form-group">
                                    <label class="col-sm-2 control-label" for="ciudad">Ciudad</label>
                                    <input class="form-control" type="text" id="ciudad" name="ciudad">
                                </fieldset>
                                  {{-- Sucursal --}}
                                <fieldset class="form-group">
                                    <label class="col-sm-2 control-label" for="otros_pac">Sucursal</label>
                                    <input class="form-control" type="text" id="otros_pac" name="otros_pac">
                                </fieldset>
                           
                             
                                <fieldset>
                                    <input type="submit" name="Filtrar" class="button btn-success"> 
                                </fieldset>
                            {{ Form::close() }}
                            @can('crear_paciente')
                     <h3>Agregar paciente</h3>
                           {{ Html::link('/paciente/create', 'Agregar paciente', array('class' => 'btn btn-default'                                   ), false)}}
                           @endcan
                </div>     
                    <div class="col-md-9  main" >

                    <h1>Pacientes - 


                    </h1>
                    
                    <div class="table-responsive">
                        <table class="table table-striped  ">
                      <thead> 
                        <tr> 
                            <th>Apellidos</th> 
                            <th>Nombres</th> 
                            <th>Cédula</th> 
                            <th>Historia</th> 
                            <th>Condición</th>
                            <th>Sucursal</th>
                        </tr> 

                      </thead> 
                      <tbody>
                        @foreach($pacientes as $paciente)
                        <tr>
                            <td>
                            {{ Html::link("paciente/$paciente->id_pac" , $paciente->apellidos_pac)}}</td>
                            <td>{{ $paciente->nombres_pac }}</td>
                            <td>{{ $paciente->cedula_pac }}</td>
                            <td>{{ $paciente->historia }}</td>
                            <td>{{ $paciente->condicion->condicion }}</td>
                            <td>{{ $paciente->otros_pac }}</td>
                            </tr>
                        @endforeach
                        </tbody>
                         </table>
                         
<div class="pagination"> 
@if((Request::only('otros_pac')!=null))
{{ $pacientes->appends(Request::only('otros_pac'))->links() }} 
@endif
</div>
                    @endcan
                            </div>
                    </div>
                </div>
       @stop   

and my route

Route::post('/paciente/filter','PacienteController@filter' );
0 likes
10 replies
jlrdw's avatar

Did you follow the example in the documentation on appending links?

jlrdw's avatar

Just do this

Appending To Pagination Links

You may append to the query string of pagination links using the appends method. For example, to append sort=votes to each pagination link, you should make the following call to appends:

{{ $users->appends(['sort' => 'votes'])->links() }}
If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method. For example, to append #foo to the end of each pagination link, make the following call to the fragment method:

{{ $users->fragment('foo')->links() }}

https://laravel.com/docs/5.3/pagination

2 likes
pag66's avatar
Level 5

Made the chance but still have the same problem.

jlrdw's avatar

In

return view ('pacientes.filter',compact('pacientes','clinica'));

Have to pass the where array similar tp

return \View('owner/pownerlist')->with('data', $data['owners'])->with('data2', $data['osearch']);

osearch is search criteria being passed for the appending to links. Build yourself the search array to keep passing Edit: Like

$data['osearch'] = array('psch' => $dogsearch, 'aval' => $aval);
1 like
pag66's avatar
Level 5

I'm using inn this way

 if ($request->has('otros_pac')){ 
          //filtrado por valor otros_pac
         //   dd($request->otros_pac);
         // dd($request);
            $pacientes = Paciente::where('otros_pac','LIKE','%'.$request->otros_pac.'%')->paginate();
            $filtro= ['otros_pac'=>$request->otros_pac];
        }
 return view ('pacientes.filter',compact('pacientes','clinica','filtro'));

on the view

@foreach($filtro as $key=>$value)
{{ $pacientes->appends([$key=>$value])->links() }} 
@endforeach

but still the same result, the second page is empty

pag66's avatar
Level 5

I solve it using your tips and also generating a get route. Thanks a lot for the help @jlrdw

farjana's avatar

I want to display categories in my front view. For example i have two categories, Men and Women. i Want the front view to display Men and Woman, when clicking on one of them perhaps men, they see only shirts for men. Can you help.

Please or to participate in this conversation.