How to filter records

Asked

Viewed 270 times

-1

public Function search1(Request $request) {

    $data = Cliente::where(
        'nome','LIKe', $request->nome . '%'
         'situacao',$request->situacao . '=')->get();

        return view('cliente.search',
        [
        'clientes'=>$data,
        'nome'=>$request->nome
        ]
    ); 

}
  • What exactly do you want to do? I noticed some errors in your Where but in order to help you better I need to understand which is the final query you want to mount...

  • public Function search1(Request $request) { $data = Client::Where( 'name','Like', $request->name . '%')->Where( 'situacao',$request->situacao . '=')->get(); if(!isset($date['situacao'])){ $data = Client::Where('situacao',$request->situation . '1'.'0')->get(); } Return view('client.search', [ 'clients'=>$date, 'name'=>$request->name, 'situacao'=>$request->situacao ] ); ; }

  • I want to do a search by name and situation the situation returns 0 and 1 only that when I leave empty the field situation I want to search the 0 and 1 as I do?

  • if(!isset($date['situation'])){ $data = Client::Where('situation',$request->situation . '=')->get(); }

1 answer

0

Do you want to make a filter by name and have the possibility to filter by situation? That’s it?

if it’s:

public function search1(Request $request) {
    $data = Cliente::where('nome','LIKE',$request->nome.'%');
    if(!empty($request->situacao)){
        $data->where('sutiacao',$request->situacao);
    }
    $data = $data->get();
    return view('cliente.search',
        [
            'clientes'=>$data,
            'nome'=>$request->nome
        ]
    ); 
}

Explaining:

We initialize the Builder query on the condition that it is required:

$data = Cliente::where('nome','LIKE',$request->nome.'%');

Note that before calling the method get() the variable $data contains an instance of the Builder query, so you can add filters or joins as needed before calling get().

That being said, we will now check the existence of the filter by situation and if it exists we will add the second filter in the query Builder:

    //if(!empty($request->situacao)){ Editado, empty() considera 0 como vazio e 
    // não entra na condição
    if(!is_null($request->situacao)){
        $data->where('sutiacao',$request->situacao);
    }

Finally, having all the filters applied, we call the get() out of the if to ensure that the query is always executed with situation or not:

$data = $data->get();

Now let’s return to view:

    return view('cliente.search',
        [
            'clientes'=>$data,
            'nome'=>$request->nome
        ]
    );

I suggest reading the documentation of the eloquen and the query Builder of the Laravel:

https://laravel.com/docs/5.8/eloquent

https://laravel.com/docs/5.8/queries

They have very detailed and simple documentation.

  • thank you! more had a small error because when selects inactive that returns zero, searches in the database all records, that in case it was only to return only the inactive

  • @Guilherme.ramos turns out that Empty considers 0 empty... in this case exchanges ! Empty for ! is_null.

  • thank you had already identified this error !!

  • Good that it worked! If my answer helped you, be sure to mark as a useful answer/answer... :)

Browser other questions tagged

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