Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Collection Attribute #23

Open
ronlinet opened this issue Apr 21, 2021 · 2 comments
Open

Update Collection Attribute #23

ronlinet opened this issue Apr 21, 2021 · 2 comments

Comments

@ronlinet
Copy link

ronlinet commented Apr 21, 2021

The bellow code is updating the 'alert_notification' User attribute but deleting all other fields.
What is the correct approach to update a collection element without destroying all other fields?

$this->firestoreServiceAccount = ServiceAccount::fromJsonFile( storage_path()  . '/app/google-services.json');
        $this->firestore = (new Factory)->withServiceAccount($this->firestoreServiceAccount)->createFirestore();

 
            $collection     = $this->firestore->collection('users')->document( (int) $user->id );
            $collection->set
            (
                [
                    'alert_notifications'             => $user->alert_notifications,
                ]
            ); 

@ronlinet ronlinet changed the title Update Feature works as required Update Feature Apr 21, 2021
@ronlinet ronlinet changed the title Update Feature Update Collection Attribute Apr 21, 2021
@ronlinet
Copy link
Author

Simply recreating all collection with updated attribute does the trick.
Working code :

$this->firestoreServiceAccount = ServiceAccount::fromJsonFile( storage_path()  . '/app/google-services.json');
        $this->firestore = (new Factory)->withServiceAccount($this->firestoreServiceAccount)->createFirestore();

 
            $collection     = $this->firestore->collection('users')->document( (int) $user->id );

            $email                   = $collection->snapshot()->data()['email'];
            $fcm_registration_token  = $collection->snapshot()->data()['fcm_registration_token'];

            $collection->set
            (
                [
                    'fcm_registration_token' => $fcm_registration_token,
                    'email'                 => $email,
                    'alert_notifications'   => $user->alert_notifications,
                ]
            );            

 

@ronlinet
Copy link
Author

ronlinet commented Apr 21, 2021

@morrislaptop should we check the Update feature checkbox in the library TODO list and include the above code in the demo code ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant