php Laravel:共享表输入的MySQL中仅显示1个数据

t98cgbkg  于 2022-12-17  发布在  PHP
关注(0)|答案(1)|浏览(133)

想问一下,为什么当我从一个表中输入数据时,MySQL中只出现了1个来自共享表输入的数据?
这是网站上表格的图片。有3个学生数据可供输入。但后来只会出现1个,在下图中用红线标出,3个数据应该出现在mysql中。

但从表中输入数据后,MySQL中只出现1个数据,MySQL中应出现3个数据。

刀片
<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>
控制器
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');
}

因此,对于控制器,我与其他表数据连接。

型号
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',
    ];
}

什么是解决方案,使当你点击完成按钮,所有的数据在MySQL出现?有什么问题,在我的编码或在MySQL?

szqfcxe2

szqfcxe21#

我对Laravel还是新手,但这可能对您有所帮助,因为当您按“完成”时,您将按多个学生ID保存数据
而不是一次就像

$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();

您应该使用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);

还是不太确定但可能会有帮助

相关问题