Home > Blockchain >  How to retrive a specific object in laravel firebase?
How to retrive a specific object in laravel firebase?

Time:06-07

I am using laravel as a framework and database as a Firebase i want to fetch the data based on the some key value ,for that i wrote following code

<?php

class TestController extends Controller
{
    private $database;
    public function __construct()
    {
        $this->database = FirebaseService::connect();
    }

    public function get(Request $request){
        $name = $request->name;  //from api request the value is test
        $data = $this->database->getReference('/users/')
                                ->orderByValue('name')
                                ->equalTo($name)->getValue();
        return response()->json(['data' => $data]);
    }
}

but it's throwing following error

Kreait\Firebase\Exception\Database\UnsupportedQuery: Index not defined, add &quot;.indexOn&quot;: &quot;.value&quot;,

can you please help me to fetch the particular value data ..? enter image description here

CodePudding user response:

You'd need to set indexOn users/{uid}/name, then query with equalTo:

$snapshot = $this->database
    ->getReference('users')
    ->orderByChild('name')
    ->equalTo($request->name)
    ->getSnapshot();
  • Related