简体   繁体   中英

How to delete element of an array on Firestore using Laravel

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消防站 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拉维

I don't know how arrayRemove works on PHP, I just want to remove this entire element? 这个指数

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:

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]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM