How to create search filters in Laravel?

Asked

Viewed 5,263 times

0

I need to create a search system that receives data through inputs, select and checkbox.

My form for research:

<form class="navbar-form navbar-left" role="search" action="{!! url('/pesquisar') !!}" method="post" style="margin-left: 25%;margin-bottom: 3%;">

    <div class="form-group">
      {!! csrf_field() !!}
      <input type="text" name="texto" class="form-control" placeholder="Pesquisar" style="width: 600px;">

    </div>
    <div id="demo" class="collapse">
      <label class="checkbox-inline">
       <input type="checkbox" id="semimagem" value="semimagem" name="semimagem"> Sem imagem
      </label>
      <select class="form-control" id="status">
        <option value="1">Ativo</option>
        <option value="0">Desabilitado</option>
      </select>
    </div>

Function search of the Controller

    public function Pesquisar()
   {
     $texto = Input::get('texto');
     $status = Input::get('status');
     $pesquisa = Produtos::where('erp_status', 'like', '%'.$status.'%')
    ->orWhere('erp_cost','like','%'.$texto.'%')
    ->orWhere('erp_productid','like','%'.$texto.'%')
    ->orWhereHas('descricao', function ($query) use ($texto) {
        $query->where('erp_name', 'like', '%'.$texto.'%');
    })
    ->orderBy('erp_status')
    ->paginate(20);

My routes:

Route::get('pesquisar','ProdutosController@pesquisar');
Route::post('pesquisar','ProdutosController@pesquisar');

Any suggestions?

  • What happened? There are mistakes?

1 answer

1


Your route can be "any" so you will receive both "get" and "post requests".

Route::any('pesquisar','ProdutosController@pesquisar');

Browser other questions tagged

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