Search code examples
phpgoogle-cloud-firestorelaravel-livewire

Putting filters into a livewire search from firestore


Using Cloud Firestore, how can I use multiple where statements in a single query? Am having BadRequestException throwing back (Only a single 'NOT_EQUAL', 'NOT_IN', 'IS_NOT_NAN', or 'IS_NOT_NULL' filter allowed per query.)

$query = $this->db->collection('accounts');

$query = $this->filterStatus && $this->filterStatus != -1 ? $query->where('status', '=', $this->filterStatus) : $query->where('status', '!=', '0');

$query = $this->filterRisk && $this->filterRisk != -1 ? $query->where('risk_level', '=', $this->filterRisk) : $query->where('risk_level', '!=', '0');

$accounts = $query->documents();

Also not sure if this is the best way to wrap where codes within firebase code


Solution

  • The problem is not so much that you have multiple conditions, but that you're using a combination of conditions that is not allowed. As both the error messages and the documentation on query limitations say:

    In a compound query, range (<, <=, >, >=) and not equals (!=, not-in) comparisons must all filter on the same field.

    And in your code you're trying to do both $query->where('status', '!=', '0') and $query->where('risk_level', '!=', '0'), which does not meet that limitation.

    You'll have to perform one of the conditions on the database, and perform the additional filtering in your application code.