2

So I have a firestore that looks like this

enter image description here

and I want to be able to get just one value from a document, like getting the "uid" from the highlighted document, my php code looks like this:

<?php

    require 'vendor/autoload.php';
    //putenv('upheld-pursuit-274606-237ac40b2662.json');
    use Google\Cloud\Firestore\FirestoreClient;

    class Firestore
    {
        protected $db;
        protected $name;
        public function __construct(string $collection)
        {
            $this->db = new FirestoreClient([
                'keyFilePath' => 'upheld-pursuit-274606-237ac40b2662.json',
                'projectId' => 'upheld-pursuit-274606'
            ]);

            $this->name = $collection;
        }


        public function getWhere(string $field, string $operator, $value)
        {
            $arr = [];
            $query = $this->db->collection($this->name)->where($field, $operator, $value)->documents()->rows();
            if(!empty($query)){
                foreach ($query as $q){
                    $arr[] = $q->data();
                }
            }
            return $arr;
        }

        public function get(string $field)
        {
            $arr = [];
            $query = $this->db->collection($this->name)->document($field)();
            if(!empty($query)){
                /*foreach ($query as $q){
                    $arr[] = $q->data();
                }*/
                throw new Exception('Document does not exist!');
            }
            return $query;
        }

    }
?>

and the code calling it looks like this:

<?php

session_start();

require("connect.php");
require_once 'vendor/autoload.php';
require_once 'Firestore.php';

$fs = new Firestore('users');
$tfs = new Firestore('tutors');


    $username = mysqli_real_escape_string($conn, $_GET['name']);

    $password = mysqli_real_escape_string($conn, $_GET['pass']);

    $password = md5($password);

    $data = array();

    print_r($fs->get('1WINXTQdshhn4jLfhMWWaZNNdL32'));

I want to be able to retrieve just one field from a document in case I need for certain if statements or other conditional statements and for checking purposes

Tristan
  • 33
  • 8

2 Answers2

0

It seems you can only do this with NodeJS package for Cloud Firestore. In this answer there's a link to the doc for firestore select which does this.

skpn
  • 89
  • 1
  • 5
0

Just as any of the other Client Libraries, the PHP google/cloud-firestore has the select method, which allows you to select the fields that you want to be retrieved.

I recommend checking the simple_queries.php example, if you need further examples on how to set up these queries.

pessolato
  • 1,472
  • 6
  • 14