Sort a search result with filter

Asked

Viewed 892 times

7

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

Example:

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

The problem is when I click order by a select form, he doesn’t keep the URL current, it simply does:

/empresas?ordenar=nome

It should be something like:

/business? status=2&type=3&sort=page=3

I made a append, like paging does, but it still didn’t work.

In the 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 the View:

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

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

And I have an ordination form, maybe the mistake 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>
  • Post your code.

  • Is that your combobox is not rendered according to Laravel, right ?

  • Exactly, and I don’t know how to put it in the append

  • I think it would be the case to be able to play the current "Gets" in the form action, right? Type action="/companies? state=2&type=3". Does this work?

  • It doesn’t work :( several sites have this "sort", but I can’t search, I don’t know the specific name. Probably have some package, or something like that.. but it’s hard to find.. rs Does anyone have any light, as you go? thanks

  • You have to add the current parameters (which are in the current url) in your form action, or add inputs of type Hidden with the current url parameters in your form.

  • @Eduardocruz solved. Take a look at my answer :). Even if you have found another solution, it might be worth looking at, as learning

  • Which version is using Laravel 4 or Laravel 5?

Show 3 more comments

1 answer

1

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

Usually I don’t use forms to create Ordenação, but only Links in table headers.

I believe one way to solve 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:

Browser other questions tagged

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