Laravel 5.1 Sort the result of a search filter
<p>I have searched the site and I can choose various filters. This already works. He returns and makes pagination correctly.</p>

<p>Example:</p>

<pre><code>/empresas?estado=2&amp;tipo=3&amp;page=2
</code></pre>

<p>The problem is when I click to order (one form select).</p>

<p>It does not maintain the current URL, it simply does:</p>

<pre><code>/empresas?ordenar=nome
</code></pre>

<p>It should be something like:</p>

<pre><code>/empresas?estado=2&amp;tipo=3&amp;ordenar=nome&amp;page=3
</code></pre>

<p>I made an append such as paging does.</p>

<p>But it did not work.</p>

<p>The Controller:</p>

<pre><code>$estados = DB::table('estados')-&gt;orderBy('nome', 'asc')-&gt;get();
$tipos = DB::table('tipos')-&gt;orderBy('nome', 'asc')-&gt;get();

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

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

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

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

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

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

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

return view("pages.empresas.index")
    -&gt;with('estados', $estados)
    -&gt;with('tipos', $tipos)
    -&gt;with(compact('empresas', 'word', 'ordenar', 'estado','tipo'));
</code></pre>

<p>In VIEW:</p>

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

<pre><code>&lt;center&gt;{!! str_replace('/?','?', $empresas-&gt;appends(Input::query())-&gt;render()) !!}&lt;/center&gt;
</code></pre>

<p>I have a form to sorting by perhaps the error is it</p>

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