@ligonsker (Untested), one way to simplify this process would be to use a recursive function to iterate through the nested array and build the query. Here is an example of how you could do this:
public function buildQuery($selections, $query)
{
foreach ($selections as $key => $value) {
if (is_array($value)) {
$query->where($key, $key);
$this->buildQuery($value, $query);
} else {
$query->orWhere($key, $value);
}
}
return $query;
}
public function filterResults($selections)
{
$query = DB::table('mytable');
$fullQuery = $this->buildQuery($selections, $query);
return $fullQuery->get();
}
The buildQuery function will recursively iterate through the nested array, adding a where clause for each key and value. When it reaches a value that is not an array (i.e. a team), it will add an orWhere clause for that value.
You can then call the filterResults function and pass in the $selections array as an argument. This will return a query that filters the results based on the selections made by the user.