Home > Software design >  undefined method where function in laravel
undefined method where function in laravel

Time:11-06

I've been trying to filter out the product. If their category is popular, it will be retrieved. However, it's an error if I use a where in my controller. I think there's a wrong in my query?

Error: Call to undefined method Google\Cloud\Firestore\QuerySnapshot::where()

Language: PHP Framework: Laravel 9 Database: Firebase, firestore

What i have tried so far:

controller:

$product = app('firebase.firestore')->database()->collection('Products')->documents();
        $query1 = $product->where('category', isEqual: "popular");
        $popular = $query1->documents();
        return view('admin.product', compact('product', 'popular'));

CodePudding user response:

Remove ->documents() from the $product as you're trying to use the where method on the result and not on the query builder which is why where is not available. Also, then you need to introduce another variable to store the query builder so that you can also get the products.

Updated code:

$query = app('firebase.firestore')->database()->collection('Products');

$product = $query->documents();

$popular = $query->where('category', isEqual: "popular")->documents();

return view('admin.product', compact('product', 'popular'));
  • Related