Search code examples
phplaravellaravel-9

Laravel: Only 1 data appears in MySQL from shared table input


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.

Screenshot of the table display on the website

But after inputting data from the table, only 1 data appears in MySQL, 3 data should appear in MySQL.

Screenshot of data display in MySQL

Blade

<div class="overflow-x-auto relative shadow-md sm:rounded-lg">
    <form method="POST" action="{{route('add.teacher.attendance.detail')}}" enctype="multipart/form-data">
        @csrf
        <table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
            <thead class="text-xs text-white uppercase bg-[#464867] dark:bg-[#464867]">
            <tr>
                <th scope="col" class="py-3 px-6">
                    NISN
                </th>
                <th scope="col" class="py-3 px-6">
                    Name
                </th>
                <th scope="col" class="py-3 px-6">
                    Attendance
                </th>
                <th scope="col" class="py-3 px-6">
                    Note
                </th>
            </tr>
            </thead>
            <tbody>
            @foreach($dataAttendanceTa as $data)
                <tr class="bg-white border-b dark:bg-gray-900 dark:border-gray-700">
                    <th scope="row" class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                        {{$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 class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                        {{$data->name}}
                    </td>
                    <td class="flex py-4 px-6 w-10 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                        <form class="flex">
                            <div class="flex items-center mb-4 ml-3">
                                <input id="present" type="radio" name="id_atte_type" value="2" class="w-4 h-4 border-gray-300 focus:ring-2 focus:ring-blue-300 dark:focus:ring-blue-600 dark:focus:bg-blue-600 dark:bg-gray-700 dark:border-gray-600" checked>
                                <label for="present" class="block ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">
                                    Present
                                </label>
                            </div>

                            <div class="flex items-center mb-4 ml-3">
                                <input id="sick" type="radio" name="id_atte_type" value="1" class="w-4 h-4 border-gray-300 focus:ring-2 focus:ring-blue-300 dark:focus:ring-blue-600 dark:focus:bg-blue-600 dark:bg-gray-700 dark:border-gray-600">
                                <label for="sick" class="block ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">
                                    Sick
                                </label>
                            </div>

                            <div class="flex items-center mb-4 ml-3">
                                <input id="absent" type="radio" name="id_atte_type" value="3" class="w-4 h-4 border-gray-300 focus:ring-2 focus:ring-blue-300 dark:focus:ring-blue-600 dark:bg-gray-700 dark:border-gray-600">
                                <label for="absent" class="block ml-2 text-sm font-medium text-gray-900 dark:text-gray-300">
                                    Absent
                                </label>
                            </div>
                        </form>
                    </td>
                    <td class="py-4 px-6 font-medium text-gray-900 whitespace-nowrap dark:text-white">
                        <input
                            type="text"
                            name="note"
                            id="note"
                            autocomplete="note"
                            value="{{ old('note') }}"
                            class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 dark:bg-gray-600 dark:border-gray-500 dark:placeholder-gray-400 dark:text-white"
                        >
                    </td>
                </tr>
            @endforeach
            </tbody>
        </table>
        <input type="submit" class="mt-3 mb-3 focus:outline-none text-white bg-[#464867] hover:bg-[#464867] font-medium rounded-lg text-sm px-5 py-2.5 mb-2 " 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?


Solution

  • 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