dimanche 29 mars 2020

How to insert and delete record in database which is using dynamic fields Laravel?

I am using Laravel 5. In this application, the staff name can be added and deleted dynamically. For creating new records, all functions run smoothly. However, I have some difficulties in updating the records. In 'edit' page, the staff names created are displayed and can be changed. These names can be updated. But, I put add button and delete button to enable user to add new staff name or delete existing staff name.

My function just can update existing record in database. But the new added staff name not inserted in database. This is my function to update in controller.

      public function update(Request $request, $id)
{

    $demo = Demo::findOrFail($id);

    $demo->tajuk_demo = $request->tajuk_demo;
    $demo->syarikat_id = $request->nama_syarikat;
    $demo->peralatan_sistem = $request->peralatan_sistem;
    $demo->tarikhmasa = $request->tarikhmasa;
    $demo->tarikhmasa_tamat = $request->tarikhmasa_tamat;
    $demo->tempat_demo = $request->tempat_demo;
    $demo->ulasan_demo = $request->ulasan_demo;
    $demo->komen_demo = $request->komen_demo;
    $demo->update();

    $pegawai = $request->all();

    // $rekod_id = $pegawai['rekod_id'];
    $itemRegistrationID = $pegawai['nama'];
    $OperasiID = $pegawai['pangkat'];
    $SectionID = $pegawai['bahagian'];

    $pegawai_semasa = Pegawai::where('rekod_id', '=', Input::get('rekod_id'))->exists();


        $peg_hadir = Pegawai::where('demo_id', '=', $id)
                 ->get();

        // Update the existing record
        foreach($peg_hadir as $peg){
        $key = array_search($peg->rekod_id, $pegawai['rekod_id']);
        $peg->itemRegistrationID = $itemRegistrationID[$key];
        $peg->OperasiID = $OperasiID[$key];
        $peg->SectionID = $SectionID[$key];
        $peg->save();
        }


    if (is_null($pegawai_semasa))
    {
        // Insert new record into database
        $count = count(Input::get('nama'));

        // get data
        $itemRegistrationID = Input::get('nama');
        $OperasiID = Input::get('pangkat');
        $SectionID = Input::get('bahagian');

        //loop through and save data
        for($i = 0; $i < $count; ++$i) {

            $pegawai = new Pegawai;
            $pegawai->demo_id = $id;
            $pegawai->itemRegistrationID = $itemRegistrationID[$i];
            $pegawai->OperasiID = $OperasiID[$i];
            $pegawai->SectionID = $SectionID[$i];
            $pegawai->save();
        }  
    }
    else
    {
      return redirect('demo');  
    }

}

This is the code in view blade for edit page:

<div class="form-group">
 <div class="row">
  <input type="button" class="add-row" value="Add">
  <button type="button" class="delete-row">Delete</button>
 </div>
</div>
<!--mula table  -->

<div class="form-group">
 <div class="row">
  <div class="col-lg-10">
    <table class="table table-striped table-bordered" id="pegawaihadir_table" >
      <thead>
        <tr>
          <td class="text-center col-lg-1"><strong>Pilih</strong></td>
          <td class="text-center col-lg-1"><strong>Pangkat</strong></td>
          <td class="text-center col-lg-3"><strong>Nama</strong></td>
          <td class="text-center col-lg-2"><strong>No Badan</strong></td>
          <td class="text-center col-lg-2"><strong>Seksyen</strong></td>
          <td class="text-center col-lg-2"><strong>No Tel</strong></td>
          <td class="text-center col-lg-2"><strong>Ext</strong></td>
        </tr>
      </thead>
        <tbody>

          @foreach($PegawaiHadir as $value)                                           
            <tr>
                <td><input type="checkbox" name="rekod" style="width:20px">
                  {!! Form::hidden('rekod_id[]', $value['rekod_id'], ['class' => 'form-control']) !!}
                </td>
                <td class="text-center"></td>
                <td>
                
                </td>
                <td class="text-center"></td>
                <td class="text-center"></td>
                <td class="text-center"></td>
                <td class="text-center"></td>
            </tr> 
          @endforeach                  
      </tbody>
    </table>              
 </div>
</div>

For dynamic fields, I am using js code to add and delete the record displayed in the page.

  <script>
count=1;
$(document).ready(function(){
    $(".add-row").click(function(){
        var markup = '<tr><td><input type="checkbox" name="rekod" style="width:20px"></td>';
        markup +=  '<td><input type="text" class="form-control pangkat"  name="pangkat[]" style="width:100px" readonly></td>';
        markup +=  '<td><select class="form-control select2 nama" name="nama[]" style="width:300px"><option value="">Pilih</option><?php foreach($pegawai as $key => $value):echo '<option value="'.$key.'">'.addslashes($value).'</option>'; endforeach; ?></select></td>';
        markup +=  '<td><input type="text" class="form-control no"  name="no[]" style="width:100px" readonly></td>';
        markup +=  '<td><input type="text" class="form-control bahagian" name="bahagian[]" style="width:100px" readonly></td>';
        markup +=  '<td><input type="text" class="form-control telefon" name="telefon[]" style="width:100px" readonly></td>';
        markup +=  '<td><input type="text" class="form-control ext" name="ext[]" style="width:100px" readonly></td></tr>';
        $("table tbody").append(markup);
        count++;

        });

    $(document).on('change', 'select.form-control.nama', function() {
        var PegID = jQuery(this).val();
        var row = $(this);
        // alert(PegID);
           if(PegID)
           {
              jQuery.ajax({
                 context: this,  
                 url : 'get_pegawai/'+PegID,
                 type : "GET",
                 dataType : "json",
                 success:function(data)
                 {
                    console.log(data);
                        $(this).closest('tr').find('td .form-control.pangkat').val(data.operasiname);
                        $(this).closest('tr').find('td .form-control.no').val(data.Nobadan);
                        $(this).closest('tr').find('td .form-control.bahagian').val(data.sectionname);
                        $(this).closest('tr').find('td .form-control.telefon').val(data.notelttp);
                        $(this).closest('tr').find('td .form-control.ext').val(data.ext);  
                 }
              });
           }
           else
           {
              $(this).closest('tr').find('td .form-control.pangkat').empty();
              $(this).closest('tr').find('td .form-control.no').empty();
              $(this).closest('tr').find('td .form-control.bahagian').empty();
              $(this).closest('tr').find('td .form-control.telefon').empty();
              $(this).closest('tr').find('td .form-control.ext').empty();  
           }

    });


    // Find and remove selected table rows
    $(".delete-row").click(function(){
        $("table tbody").find('input[name="rekod"]').each(function(){
            if($(this).is(":checked")){
                $(this).parents("tr").remove();
            }
        });


    });



});    
</script>

I also doesn't know how to identify the selected staff name to be deleted from existing record in database. I am providing a complete code to ease anyone who want to help to understand better. Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire