I am new to MongoDB
and i am using jenssegers/laravel-mongodb
package for build an API.
I am facing problem while updating and deleting a records in array.
Here is My code of update using foreach
foreach ($event->volunteers as $key => $volunteer) {
if ($volunteer['user_id'] == $request->user_id) {
$event->where('volunteers.user_id', $request->user_id)
->update(["volunteers.$key.attendance_status" => $request->attendance_status
]);
}
}
Here is same code with Pull
Event::where('volunteers.user_id', $request->user_id)->push('volunteers', [
'attendance_status' => $request->attendance_status
], true);
Here is the code of unset
foreach ($event->volunteers as $key => $volunteer) {
if ($volunteer['user_id'] == $request->user_id) {
$event->where('volunteers.user_id', $request->user_id)
->unset(
"volunteers.$key.attendance_status");
}
}
None of them are working
My DB structure is
"volunteers" : [
{
"user_id" : NumberInt(1),
"name" : "Prafful K Panwar",
"skills" : [
"Plumber",
"Carpenter"
],
"incentives" : {
"tshirt_size" : "XXL"
},
"ready_to_contribute_min_amount" : "Yes",
"attendance_status" : false
},
{
"user_id" : NumberInt(2),
"name" : "Prafful Panwar",
"skills" : [
"Plumber"
],
"incentives" : {
"tshirt_size" : "XXL"
},
"ready_to_contribute_min_amount" : "Yes",
"attendance_status" : false
}
]
Any advice, help and suggestions will be greatly appreciated.
Thank you :)