mercredi 2 juin 2021

How to apply filter conditions gradually without breaking the previous filtering conditions

Maybe this question seems stupid but I need to learn how it is possible to fix it. In Laravel, I try to apply many conditions according to users choices, like this:

$query = Model::query();
if ($request->has('ids')) {
    $query = $query->whereIn('id', $request->get('ids'));
}
if ( ($request->has('fruits')) ) {
    $query = $query->whereHas('fruits', function ($sQuery) use ($list) {
       $sQuery->whereIn('fruit.id', $list);
    });
}
if ($request->has('date')) {
   $query = $query->whereDate('created_at', '>=', $request->get('date'))
              ->orWhereDate('updated_at', '>=', $request->get('date'));
}

The fact is the conditions about dates break previous filter conditions. I am getting other raws which do not match to first conditions. I guess it is because orWhereDate which is applied as condition "or" about all others. How to deal with this?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire