Hello guys I am trying to do a mass update for my application using a checkbox but when I try to update it will just update the 1st student id(I tried to select other students the update does not work). Could you give some insights or concrete examples I can follow?
This is what I have tried so far
public function changeSched()
{
DB::table('class_list')
->whereIn('id', $this->selectedStudents)
->update(['section' => $this->selectedSched]);
}
For my checkbox
@foreach($classlist as $stud)
<tr>
<th class="bg-white py-5 px-5">
<div class="items-center">
<input type="checkbox" wire:model="selectedStudents.{{ $stud->id }}"/>
</div>
</th>
CodePudding user response:
I do not see any error in your update statement so it should update all the records provided by the query (mass updates are documented here).
I guess the error is before, when you set $this->selectedStudents
. Are you sure that this variable contains a simple array (not an associative array) with all the selected IDs?
You should do the following:
- Debug the
$this->selectedStudents
variable to ensure its content is correct - Temporarily replace it by a hardcoded value
[1, 2, 3]
to ensure that the rest of your code is appropriately working
UPDATE
To convert $this->selectedStudents
in the right format, you can do the following:
$selectedIds = [];
foreach ($this->selectedStudents as $id => $isSelected){
if ($isSelected) {
$selectedIds[] = $id;
}
}
DB::table('class_list')
->whereIn('id', $selectedIds)
->update(['section' => $this->selectedSched]);