Ask about bd with conditional?

2

The question I have come up with is simple;

I make a query with \DB

$data =  \DB::table('users')->select($lo_que_sea)
->where(['vacations.aceptado' => $filtro])
->get();

I want to make a condition on that where so that if $filtro is null, do not do that where something like this:

$data =  \DB::table('users')->select(lo que sea)
if($filtro != null){
    ->where(['vacations.aceptado' => $filtro])
    ->get();
}else{
    ->get();
}

If I do not just do the ->get() , right now I use this query dynamically in two methods, but if I could do the conditional I would not have to create another method or another query. To see all the inserts.

Greetings and thanks.

    
asked by Peisou 23.08.2018 в 11:13
source

1 answer

3

I think you've practically done it already:

$query =  \DB::table('users')->select(lo que sea);
if($filtro != null) {
   $data = $query->where(['vacations.aceptado' => $filtro])
     ->get();
} else {
   $data = $query->get();
}
    
answered by 23.08.2018 / 11:59
source