I want to store multiple data to database because i have 2 select option with same name. i've following some tutorial from google and youtube but i still can't figured out how to do it.
Table structure:
id | nama_mapel | guru_id | kode_mapel | ki_kd_id |
I have view like this, there's 2 select option with same name:
<input type="text" name="nama_mapel[]">
<input type="text" name="kode_mapel[]">
<select type="text" name="guru_id[]">
<option value disable>Pilih Guru</option>
@foreach ($guru as $item)
<option value="{{ $item->id }}">{{ $item->nama_guru }}</option>
@endforeach
</select>
<select name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Dasar</option>
@foreach ($komp_dasar as $kd)
<option value="{{ $kd->id }}">{{ $kd->kompetensi }}</option>
@endforeach
</select>
<select type="text" name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Inti</option>
@foreach ($komp_inti as $ki)
<option value="{{ $ki->id }}">{{ $ki->kompetensi }}</option>
@endforeach
</select>
and this is my controller:
public function store(Request $request)
{
$nama_mapel = $request->nama_mapel;
$guru_id = $request->guru_id;
$kode_mapel = $request->kode_mapel;
$ki_kd_id = $request->ki_kd_id;
foreach ($nama_mapel as $row => $key){
$data= [
'nama_mapel' => $nama_mapel[$row],
'guru_id' => $guru_id[$row],
'kode_mapel' => $kode_mapel[$row],
'ki_kd_id' => $ki_kd_id[$row],
];
DB::table('mapel')->insert($data);
}
return redirect()->back();
}
Any help would be very appreciated, and sorry for my bad english.
$row => $keyas that's not what you are storing.$rowis actually the key, and$keyis the value for that key.