Search Form Laravel 5.5

2

I would like to know how to filter my bank data with a form where it contains more than 1 field to filter, I could only do one field only.

Form:

<form action="{{ url('panel/brands') }}" method="GET" name="frm_filter_brand" role="search">
    <div class="row ls-clearfix">
        <div class="ls-box-filter">
            <div class="row ls-clearfix">
                <div class="col-md-3">
                    <label class="ls-label">
                        <b class="ls-label-text">ID</b>
                        <div class="ls-field-md">
                            <input class="uppercase" name="id" id="id" placeholder="Ex.: 10" type="text" value="">
                        </div>
                    </label>
                </div>
                <div class="col-md-3">
                    <label class="ls-label">
                        <b class="ls-label-text">Nome</b>
                        <div class="ls-field-md">
                            <input class="uppercase" name="name" id="name" placeholder="Ex.: FORD" type="text" value="">
                        </div>
                    </label>
                </div>
                <div class="col-md-3">
                    <label class="ls-label">
                        <b class="ls-label-text">Descrição</b>
                        <div class="ls-field-md">
                            <input class="uppercase" name="description" id="description" placeholder="Ex.: Novas" type="text" value="">
                        </div>
                    </label>
                </div>
                <div class="">
                    <label style="margin-top:16px" class="ls-label ls-float-right">
                        <label class="ls-label">
                            <button id="btn-filter" type="submit" class="ls-btn ls-btn-filtrar">Filtrar</button>
                        </label>
                    </label>
                </div>
            </div>
        </div>
    </div>
</form>

My controller:

public function index(Request $request)
    {            
        $filter_id = $request->id;
        if($filter_id){
            $brands = $this->brand::where('id', $filter_id)->get();
        }else{
            $brands = $this->brand::all();
        }      
        return view('brand.index', compact('brands'));
    }

So I would like to check what values I have entered in the filters and search them.

    
asked by anonymous 09.10.2017 / 19:57

1 answer

1

You can pass an array of conditions within the where of your Query Builder using the input variables of your form. Maybe something like:

public function index(Request $request)
{            
    $filter_id = $request->id;
    $filter_name = $request->name;
    $filter_description = $request->description;

    // cria o array que será utilizado no query builder
    $filter_all;

    // verifica se veio id
    if($filter_id) {
        $filter_all[] = ['id', '=', $filter_id];
    }

    // verifica se veio name
    if($filter_name) {
        $filter_all[] = ['name', 'like', '%'.$filter_name.'%'];
    }

    // verifica se veio o description
    if($filter_description) {
        $filter_all[] = ['description', 'like', '%'.$filter_description.'%'];
    }

    // verifica se há valores para utilizarmos no 'where'
    if(isset($filter_all)){
        $brands = $this->brand::where($filter_all)->get();
    }else{
        $brands = $this->brand::all();
    }      

    return view('brand.index', compact('brands'));

}
    
09.10.2017 / 22:47