Sort the result of a search with filter

7

I have a search on the site and I can opt for several filters and this already works by returning and paging correctly.

Example:

  

/empresas?estado=2&tipo=3&page=2

The problem is when I click to sort by a select form , it does not hold the current URL , it simply does:

  

/empresas?ordenar=nome

Should be something like:

  

/ companies? status = 2 & type = 3 & sort = name & page = 3

I made a append , as the pagination does, but it still did not work.

No Controller :

$estados = DB::table('estados')->orderBy('nome', 'asc')->get();
$tipos = DB::table('tipos')->orderBy('nome', 'asc')->get();

$word = Input::get('query');
$ordenar = Input::get('ordenar');
$estado = Input::get('estado');
$tipo = Input::get('tipo');

$query = Empresas::with('usuario')->with('estado');

if($word)
    $query->where('descricao', 'LIKE', '%' . $word . '%')
          ->orWhere('carta', 'LIKE', '%' . $word . '%');

if($estado)
    $query->whereIn('estado_id', $estado);

if($tipo)
    $query->where('tipo_id', $tipo);

if($ordenar)
    $query->orderby($ordenar, 'asc');

$empresas = $query->orderBy('updated_at', 'desc')->paginate(18);

return view("pages.empresas.index")
        ->with('estados', $estados)
        ->with('tipos', $tipos)
        ->with(compact('empresas', 'word', 'ordenar', 'estado','tipo'));

In View :

I make a foreach bringing the fields normally and I make append with the render that already works

<center>
     {!! str_replace('/?','?', $empresas->appends(Input::query())->render()) !!}    
</center>

And I have a form to sort, maybe the error is in it

<form action="/empresas" method="get">
    <select class="form-control m-b" name="ordenar" onchange="this.form.submit()">
       <option value="" disabled="" selected="">Ordenar por...</option>
       <option value="nome">Nome</option>
     </select>
</form>
    
asked by anonymous 29.01.2016 / 14:25

1 answer

1

I already understood what is happening. As you are using the form, only the data present in this form will be sent to URL .

I usually do not use forms to create Ordenação , but only Links in the table headers.

I think one way to resolve this is by including the previous querystrings in a input:hidden on that form.

<form action="/empresas" method=get>
     <select class="form-control m-b" name="ordenar" onchange="this.form.submit()">
     <option value="" disabled="" selected="">Ordenar por...</option>
     <option value="nome">Nome</option>                                                       
     </select>

     @foreach(Input::except('ordenar') as $name => $value)
         {{ Form::hidden($name, $value) }}
     @endforeach

</form>

Example:

    
29.04.2016 / 14:04