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.
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...
– Jorge Lima
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 ] ); ; }
– guilherme.ramos
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?
– guilherme.ramos
if(!isset($date['situation'])){ $data = Client::Where('situation',$request->situation . '=')->get(); }
– guilherme.ramos