1

I'm trying to update an element inside of an array on Cloud Firestore. I know that Firestore still don't have the ability to update a single item of an element of the array. Instead, we can delete the entire element and then add the entire updated element again.

I have managed to add entire element but I don't know how to delete the old element.

In my case, I have a list of data in a table with a button of each row. The button is to update the status in the element of the array.

Here is my function

public function store(Request $request)
{
    $index = $request->index; //0, 1, 2, 
    $userid = $request->userid;
    $ipt = $request->ipt;
    $faculty = $request->faculty;
    $course = $request->course;
    $status = $request->status;

    //remove old element by searching the index
    $applyIptRef = $this->database->collection('applyIpt')->document($userid);
    $applyIptRef->update([
        'appliedIpt' => FieldValue::arrayRemove([$index])
    ]);

    //add the updated element
    $applyIptRef->set([
        'appliedIpt' => FieldValue::arrayUnion([[
            'appliedIPTName' => $ipt,
            'appliedIPTFaculty' => $faculty,
            'appliedIPTCourse' => $course,
            'appliedIPTStatus' => $status,
        ]])
    ], ['merge' => true]);
}

Here my firestore data looks like firestore That is the status that I want to update. So to update we need to update the entire array right? In this case, how to delete appliedIpt[0]?

UPDATED status For Priyashree Bhadra's question

I have tried sending this code

'appliedIpt' => FieldValue::arrayRemove([$index])

I got an error of

Field data must be provided as a list of arrays of form `[string|FieldPath $path, mixed $value]`

And according to the documentation for PHP, it have required parameters which are path and value. So I updated the code to this and nothing happens after executing this code.

['path' => 'appliedIpt', 'value' => FieldValue::arrayRemove([$index])]

Here is my data looks like on Laravel laravel

I don't know how arrayRemove works on PHP, I just want to remove this entire element? this index

2
  • I think arrayRemove should work. What is the error you are getting? Commented Feb 21, 2022 at 12:07
  • 1
    @PriyashreeBhadra , I have updated my question with the answer to your question. Commented Feb 22, 2022 at 8:06

2 Answers 2

1

I am not well versed with php. In JS below is the tested code for deleting the older element from the array:

async function updateData() {

  const document=doc(db, ‘applyIpt’, ‘my-doc’);
  const data = await getDoc(document);

    await updateDoc(document,{appliedIpt:arrayRemove(data.data()[‘appliedIpt’][0])});
}

updateData();

You just need to do the same in php and you will be able to delete the older element from the array. Deletion, addition, etc. are supported via the value (not the index), maybe that is the mistake you are making in your code. Here is a code example from the public documentation, just try the same with arrayRemove([‘appliedIpt’][0]) instead of arrayRemove([‘east_coast’]) and see if it works.

References for arrayRemove in php Client for Firestore :

Sign up to request clarification or add additional context in comments.

1 Comment

I have solved my own problem. I really appreciate your help.
0

I have solved my own problem. I just decoded my encoded data as element and push it to the array so that it will find the correct element in the database.

Also I found that the way I put my data into the Firestore mostly dynamic. So by using json_decode to decode the encoded json make my data as dynamic.

If there's any possible way to do other than this solution, I welcome your suggestions.

public function store(Request $request)
{
    $userid = $request->userid;
    $ipt = $request->ipt;
    $faculty = $request->faculty;
    $course = $request->course;
    $oldstatus = $request->oldstatus;
    $newstatus = $request->newstatus;

    $currentData = json_decode(json_encode([
        'appliedIPTName' => $ipt,
        'appliedIPTFaculty' => $faculty,
        'appliedIPTCourse' => $course,
        'appliedIPTStatus' => $oldstatus
    ]));

    $newData = [
        'appliedIPTName' => $ipt,
        'appliedIPTFaculty' => $faculty,
        'appliedIPTCourse' => $course,
        'appliedIPTStatus' => $newstatus
    ];

    $applyIptRef = $this->database->collection('applyIpt')->document($userid);

    //remove old element
    $applyIptRef->update([[
        'path' => 'appliedIpt',
        'value' => FieldValue::arrayRemove([$currentData])
    ]]);

    //add the updated element
    $applyIptRef->set([
        'appliedIpt' => FieldValue::arrayUnion([$newData])
    ], ['merge' => true]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.