How to create search filters in Laravel?

0

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

My form to search:

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

      

Controller search function

    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?

    
asked by anonymous 22.03.2017 / 15:02

1 answer

2

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

Route::any('pesquisar','ProdutosController@pesquisar');
    
08.06.2017 / 20:14