How can I store the conditions of a query Laravel

  laravel, laravel-8

How can I store the conditions of a query into a variable so that I can call it later? I have a get method that returns a Builder, not a collection.

if($a == 1) {
  $query = Review::query()
                  //many completely different conditions
                  ->where('product_id', $data['product_id']);
}
if($a == 2) {
  $query = Review::query()
                  //many completely different conditions
                  ->where('product_id', $data['product_id']);
}
$reviews = $query->get();

Source: Laravel

Leave a Reply