Laravel Search Form 5.5

Asked

Viewed 1,472 times

2

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

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 which values I entered in the filters and do the search with the same.

  • You want to create more than one condition with the where using the form inputs, that’s it?

  • That’s right, but the way you indicated, if I filter only by "ID" will not enter the right condition? if($filter_id && $filter_name && $filter_description){

  • The idea is that you have the three variables to do the where correctly. For this reason I modified the condition. Variables may come empty?

  • yes can, I think the case would be: -receive the form -verify which were entered -perform the search only with the informed ones

  • I altered, check if it works...

  • very well, it worked perfectly, thanks for the bio help! abç

Show 1 more comment

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'));

}

Browser other questions tagged

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