Skip to content
Advertisement

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 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

Advertisement

Answer

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]);
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement