Want to ask, why is it that when I input data from a table, only 1 data appears in MySQL from the shared table input?
Here is a picture of the table on the website. And there are 3 student data for input. but later only 1 will appear which is given a red line in the image below, 3 data should appear in mysql.
But after inputting data from the table, only 1 data appears in MySQL, 3 data should appear in MySQL.
Blade
<div >
<form method="POST" action="{{route('add.teacher.attendance.detail')}}" enctype="multipart/form-data">
@csrf
<table >
<thead >
<tr>
<th scope="col" >
NISN
</th>
<th scope="col" >
Name
</th>
<th scope="col" >
Attendance
</th>
<th scope="col" >
Note
</th>
</tr>
</thead>
<tbody>
@foreach($dataAttendanceTa as $data)
<tr >
<th scope="row" >
{{$data->username}}
<input
type="hidden"
name="id_atte"
id="id_atte"
value="{{$data->id_atte}}"
>
<input
type="hidden"
name="id_student"
id="id_student"
value="{{$data->id}}"
>
</th>
<td >
{{$data->name}}
</td>
<td >
<form >
<div >
<input id="present" type="radio" name="id_atte_type" value="2" checked>
<label for="present" >
Present
</label>
</div>
<div >
<input id="sick" type="radio" name="id_atte_type" value="1" >
<label for="sick" >
Sick
</label>
</div>
<div >
<input id="absent" type="radio" name="id_atte_type" value="3" >
<label for="absent" >
Absent
</label>
</div>
</form>
</td>
<td >
<input
type="text"
name="note"
id="note"
autocomplete="note"
value="{{ old('note') }}"
>
</td>
</tr>
@endforeach
</tbody>
</table>
<input type="submit" value="Done" >
</form>
</div>
Controllers
public function ViewAttendance($id){
$dataAttendance = Attendance::findOrFail($id);
$dataAttendanceTa = Attendance::join('subjects', 'attendances.id_subject', '=', 'subjects.id_sub')
->join('class_infos', 'subjects.id_class', '=', 'class_infos.id')
->join('class_details', 'class_infos.id', '=', 'class_details.id_class')
->join('users', 'class_details.id_user', '=', 'users.id')
->where('attendances.id_atte', '=', $id)
->get();
return view('teacher.attendance.data_attendance_detail', compact( 'dataAttendance', 'dataAttendanceTa'));
}
public function AddAttendanceDetail(Request $request) {
$addAttendanceDetail = new AttendanceDetail();
$request->validate([
'id_atte' => ['required'],
'id_student' => ['required'],
'id_atte_type' => ['required'],
]);
$addAttendanceDetail->id_atte = $request->input('id_atte');
$addAttendanceDetail->id_student = $request->input('id_student');
$addAttendanceDetail->id_atte_type = $request->input('id_atte_type');
$addAttendanceDetail->note = $request->input('note');
$addAttendanceDetail->save();
return redirect('teacher/data-attendance');
}
So for Controllers I join with other table data.
Model
class AttendanceDetail extends Model
{
use HasFactory;
protected $primaryKey = 'id_atte_detail';
protected $table = 'attendance_details';
protected $fillable = [
'id_atte_detail',
'id_atte',
'id_student',
'id_atte_type',
'note',
];
}
What is the solution so that when you click the DONE button, all the data in MySQL appears? and is there something wrong in my coding or in mysql?
CodePudding user response:
I'm still new to Laravel but this might be able to help you since, as you press "DONE" you are saving data by multiple student ID's
Instead of doing it once like
$addAttendanceDetail->id_atte = $request->input('id_atte');
$addAttendanceDetail->id_student = $request->input('id_student');
$addAttendanceDetail->id_atte_type = $request->input('id_atte_type');
$addAttendanceDetail->note = $request->input('note');
$addAttendanceDetail->save();
You should use foreach
$data = $request->all();
$finalArray = [];
foreach ($data as $key => $value) {
array_push(
$finalArray,
[
'id_atte' => $value['id_atte'],
'id_student' => $value['id_student'],
'id_atte_type' => $value['id_atte_type'],
'note' => $value['note']
]
);
});
AttendanceDetail::insert($finalArray);
Still, not really sure, but might be helpful