Chapter 11 - Update data to Firebase using Laravel PHP

In this chapter, we will learn how to update data to Firebase Cloud Firestore using Laravel PHP package.

Consider the list of Student in Firebase Cloud Firestore. We would update these student data for all different type of datatypes of fields.



Update normal string field name


$student = app('firebase.firestore')->database()->collection('Student')->document(<studentid>);  

Example

$student = app('firebase.firestore')->database()->collection('Student')->document('defT5uT7SDu9K5RFtIdl')  
 ->update([  
  ['path' => 'name', 'value' => 'John Kone']  
 ]);  


After update, firebase will look like this



Before updating or even inserting a value for fields like Integer or float, boolean make sure you convert your request params to specific datatype

$student = app('firebase.firestore')->database()->collection('Student')->document('defT5uT7SDu9K5RFtIdl')  
 ->update([  
  ['path' => 'id', 'value' => intval($request->id)]  
 ]);  
 $student = app('firebase.firestore')->database()->collection('Student')->document('defT5uT7SDu9K5RFtIdl')  
 ->update([  
  ['path' => 'percentage', 'value' => floatval($request->percentage)]  
 ]);  
To insert a element in to array field through update query. It will check for duplicate entries too. Consider we have hobbies of student



Now you have to insert 'Baseball' into 'hobbies' array field, we will use arrayUnion

$student = app('firebase.firestore')->database()->collection('Student')->document('defT5uT7SDu9K5RFtIdl')  
 ->update([  
  ['path' => 'hobbies', 'value' => \Google\Cloud\Firestore\FieldValue::arrayUnion(['Baseball'])]  
 ]);  




Now if you want to remove an element, then we will use arrayRemove

$student = app('firebase.firestore')->database()->collection('Student')->document('defT5uT7SDu9K5RFtIdl')  
 ->update([  
  ['path' => 'hobbies', 'value' => \Google\Cloud\Firestore\FieldValue::arrayRemove(['Music'])]  
 ]);  



Update a timestamp value by following

$student = app('firebase.firestore')->database()->collection('Student')->document('defT5uT7SDu9K5RFtIdl')  
 ->update([  
  ['path' => 'last_updated_at', 'value' => new \Google\Cloud\Core\Timestamp(new \DateTime('2020-04-04 23:03'))]  
 ]);  



To update a reference field. Example a number subject 'English' has been added and you have to update student's favourite_subject to reference of this newly added one.

$subject = app('firebase.firestore')->database()->collection('Subject')->newDocument();  
 $subject->set(['name' =>'English');  
 $student = app('firebase.firestore')->database()->collection('Student')->document('defT5uT7SDu9K5RFtIdl')  
 ->update([  
  ['path' => 'favourite_subject', 'value' => $subject]  
 ]);  


Hope you are now ready to update any datatype fields in the Firebase through your Laravel PHP code. Please let us know, how much you like the chapters of our blog and also write any doubt in comment section below. Thanks for reading.

Post a Comment

9 Comments

  1. Whoaaa thank you very much for this simple tutorial, I'm very happy to find it, because it really helped me to complete my assignment. I will wait for your next tutorial. #fromindonesia

    ReplyDelete
    Replies
    1. Hi dey, we are very much happy that our blog helped you and you completed your assignment.

      You can also help us by just following us and share this blog to reach more and more people.


      Delete
  2. Hi dey, we are very much happy that our blog helped you and you completed your assignment. Please share our blog with whoever is in this field.

    ReplyDelete
  3. how update a element of array? this post said how to remove and add, but update not

    ReplyDelete
    Replies
    1. Hi Unknown, thanks for contacting us.

      Currently there is no method in firebase to update a value by index in array field.

      For updating you have to read the array first, then update the values you want to update, and then update whole array.

      Please make sure to do that in transaction if required.

      If it helped you, please share and click follow on right menu.

      Delete
  4. Hi how to update with the use of the user input?

    ReplyDelete
  5. There's an error: Field data must be provided as a list of arrays of form `[string|FieldPath $path, mixed $value]`

    ReplyDelete