The above shown is my firebase realtime db data structure. Currently there is only one set of data under the msgs
array with the name 119
.My requirement is to add more sets of data(which may come with the index 120,121 etc.) like the array 119
.The below laravel code replaces the entire array of msgs
while adding the new set of data. I wanted to keep all the items in the msgs
array as all other arrays as it is and add new set of data as the next item of msgs
array in the firebase. How to achieve this? ANy help is appreciated.
$url = env('FIREBASE_DATABASE_URL') . 'messages.json';
// $postdat = array();
$postdat[$request->chat_id]["user_id_" . $user_id]['chats']['chat_id_' . $request->chat_id]['msgs'][$message['id']]['msg_id'] = $message['id'];
$postdat[$request->chat_id]["user_id_" . $user_id]['chats']['chat_id_' . $request->chat_id]['msgs'][$message['id']]['sender_id'] = $message['sender_id'];
$postdat[$request->chat_id]["user_id_" . $user_id]['chats']['chat_id_' . $request->chat_id]['msgs'][$message['id']]['msg_body'] = $message['message'];
$postdat[$request->chat_id]["user_id_" . $user_id]['chats']['chat_id_' . $request->chat_id]['msgs'][$message['id']]['sender_name'] = $message['sender_name'];
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postdat));
Means, i just wanted to add a new item 120
to the msgs
array .
sample data structure of expected output
2
Answers
I think you need here a PUSH data, not REPLACE data.
Modified code below;
I see also that your using
cURL
to interact with Firebase, so you’ll need to use thePOST
instead ofPATCH
to appends new data to the specified path.Firebase’s
PATCH
operation does not perform a deep merge of the data you provide with the existing data. Rather it loops over the top-level keys in the JSON you provide, and replaces the existing data for those keys with what you provide.—
In your case that means that you’re replacing the existing data in
$postdat[$request->chat_id]
(since that’s the top-level key in the JSON you post) with the value in your request, replacing all existing messages.To allow your use-case, you have two options:
You can run the
PATCH
result on a lower-level in the database. To do this, you’d post to a URL deeper in the database.This would look something like
https//yourproject.firebaseio.com/messages/72/user_id_4/chats/chat_id_72/msgs.json
(I’m using hard-coded keys here for simplicity, but you can construct the path from variables as you do now). When youPATCH
on that level, the new message ID141
will be the top-level key, so no data above that path will be replaced.Alternatively you can use
/
inside the$postdat
variable, to have Firebase perform a deeper updates.So this’d look something like
$postdat['messages/72/user_id_4/chats/chat_id_72/msgs'] = ...
. This is an instruction to Firebase to write to that specific deeper path, rather than replacing the top-level key.