1

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.

4
  • The update method allows you to insert multiple rows at once. You can do that rather than multiple calls in foreach. Commented Jun 24, 2021 at 16:52
  • Is that a good thing to do? Commented Jun 24, 2021 at 16:59
  • Well, it means you can get rid of the foreach loop, and only send one query to the database. It's up to you to determine if that's a good thing. Check the documentation. Commented Jun 24, 2021 at 17:01
  • Also, it's a really bad idea, from a standards point of view, to write $row => $key as that's not what you are storing. $row is actually the key, and $key is the value for that key. Commented Jun 24, 2021 at 17:04

1 Answer 1

1

I have found out how to do this.

For view it like this, only use array [] in ki_kd_id name. The reason is because the others is same. I have duplication for all fields except ki_kd_id.

<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 for the controller, it will be like this:

public function store(Request $request)
{
    foreach ($request->ki_kd_id as $value){
        if ($value) {
            DB::table('mapel')->insert([
                'nama_mapel' => $request->nama_mapel,
                'guru_id' => $request->guru_id,
                'kode_mapel' => $request->kode_mapel,
                'ki_kd_id' => $value,
            ]);
        }
    }
    return redirect()->back();
}

Use foreach to loop the request based on how many ki_kd_id that the request have. and then for the value just use $value as representative of ki_kd_id

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.