samedi 30 janvier 2016

Laravel 5.1 Sort the result of a search filter

I have searched the site and I can choose various filters. This already works. He returns and makes pagination correctly.

Example:

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

The problem is when I click to order (one form select).

It does not maintain the current URL, it simply does:

/empresas?ordenar=nome

It should be something like:

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

I made an append such as paging does.

But it did not work.

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 VIEW:

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

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

I have a form to sorting by perhaps the error is 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>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire