Multiple insert checkbox tapi yang terinsert ke database cuma 1 value codeigniter 4

Controller

    public function simpan()
    {
        if (!$this->request->isAJAX()) {
            throw \CodeIgniter\Exceptions\PageNotfoundException::forPageNotFound('Maaf Halaman Tidak Ditemukan');
        } else {
            $rules = [
                'assy' => [
                    'rules' => 'required|min_length[11]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 11 karakter'
                    ]
                ],
                'np' => [
                    'rules' => 'required|min_length[4]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 4 karakter'
                    ]
                ],
                'sisi' => [
                    'rules' => 'required|min_length[1]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 1 karakter'
                    ]
                ],
                'no' => [
                    'rules' => 'required|min_length[2]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 2 karakter'
                    ]
                ],
                'shift' => [
                    'rules' => 'required|min_length[1]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 1 karakter'
                    ]
                ],
                'fourm' => [
                    'rules' => 'required',
                    'errors' => [
                        'required' => 'harus diisi'
                    ]
                ],
                'problem' => [
                    'rules' => 'required',
                    'errors' => [
                        'required' => 'harus diisi'
                    ]
                ],
                'jam' => [
                    'rules' => 'required|min_length[1]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 1 karakter'
                    ]
                ],
                'who' => [
                    'rules' => 'required|min_length[2]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 2 karakter'
                    ]
                ],
                'where' => [
                    'rules' => 'required|min_length[2]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 2 karakter'
                    ]
                ],
                'why' => [
                    'rules' => 'required|min_length[10]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 10 karakter'
                    ]
                ],
                'counter' => [
                    'rules' => 'required|min_length[10]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 10 karakter'
                    ]
                ],
                'pic' => [
                    'rules' => 'required|min_length[3]',
                    'errors' => [
                        'required' => 'harus diisi',
                        'min_length' => 'minimal 3 karakter'
                    ]
                ],
                'due_date' => [
                    'rules' => 'required',
                    'errors' => [
                        'required' => 'harus diisi'
                    ]
                ]
            ];
            if (!$this->validate($rules)) {
                $errors = [
                    'assy' => $this->validation->getError('assy'),
                    'np' => $this->validation->getError('np'),
                    'sisi' => $this->validation->getError('sisi'),
                    'no' => $this->validation->getError('no'),
                    'shift' => $this->validation->getError('shift'),
                    'fourm' => $this->validation->getError('fourm'),
                    'problem' => $this->validation->getError('problem'),
                    'jam' => $this->validation->getError('jam'),
                    'who' => $this->validation->getError('who'),
                    'where' => $this->validation->getError('where'),
                    'why' => $this->validation->getError('why'),
                    'counter' => $this->validation->getError('counter'),
                    'pic' => $this->validation->getError('pic'),
                    'due_date' => $this->validation->getError('due_date')
                ];
                $output = [
                    'status' => FALSE,
                    'errors' => $errors
                ];
                echo json_encode($output);
            } else {
                $this->reportModel->save([
                    'assy' => $this->bersihkan($this->request->getVar('assy')),
                    'np' => $this->bersihkan($this->request->getVar('np')),
                    'sisi' => $this->bersihkan($this->request->getVar('sisi')),
                    'no' => $this->bersihkan($this->request->getVar('no')),
                    'shift' => $this->bersihkan($this->request->getVar('shift')),
                    'fourm' => $this->bersihkan($this->request->getVar('fourm')),
                    'problem' => $this->bersihkan($this->request->getVar('problem')),
                    'jam' => $this->bersihkan($this->request->getVar('jam')),
                    'who' => $this->bersihkan($this->request->getVar('who')),
                    'where' => $this->bersihkan($this->request->getVar('where')),
                    'why' => $this->bersihkan($this->request->getVar('why')),
                    'counter' => $this->bersihkan($this->request->getVar('counter')),
                    'pic' => $this->bersihkan($this->request->getVar('pic')),
                    'due_date' => $this->bersihkan($this->request->getVar('due_date'))
                ]);

                $psn = 'Report berhasil disimpan';
                echo json_encode(['status' => TRUE, 'psn' => $psn]);
            }
        }
    }

View

<?= form_open('employee/home/simpan', ['id' => 'report']) ?>
<div class="card-body">
    <div class="card card-primary">
        <div class="card-header">
            <h3 class="card-title">Form Tambah Data Defect</h3>
        </div>
        <div class="card-body">
            <div class="chart">
                <?= csrf_field(); ?>
                <div class="form-group">
                    <label for="sisi">Sisi</label>
                    <input type="text" name="sisi" id="sisi" class="form-control" placeholder="Sisi A atau B">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="no">No Conveyor</label>
                    <input type="text" name="no" id="no" class="form-control" placeholder="Tulis No Conveyor">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="shift">Shift</label>
                    <input type="text" name="shift" id="shift" class="form-control" placeholder="Shift 1 atau 2">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="assy">No Assy</label>
                    <input type="text" name="assy" id="assy" class="form-control" placeholder="Isi No Assy">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="np">No NamePlate</label>
                    <input type="text" name="np" id="np" class="form-control" placeholder="Isi No NamePlate">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="fourm">4M Change</label><br>
                        <input type="checkbox" name="fourm" id="fourm" value="Machine"> Machine<br>
                        <input type="checkbox" name="fourm" id="fourm" value="Methode"> Methode<br>
                        <input type="checkbox" name="fourm" id="fourm" value="Material"> Material<br>
                        <input type="checkbox" name="fourm" id="fourm" value="Man"> Man<br>
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="problem">Problem</label>
                    <select name="problem" id="problem" class="form-control">
                        <option value="">--- Pilih Problem ---</option>
                        <?php foreach ($defect as $lvl) : ?>
                            <option value="<?= $lvl->id ?>"> <?= $lvl->nama_defect ?> </option>
                        <?php endforeach ?>
                    </select>
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="jam">Jam Ke - </label>
                    <input type="text" name="jam" id="jam" class="form-control" placeholder="isi jam produksi">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="who">Pembuat Defect </label>
                    <input type="text" name="who" id="who" class="form-control" placeholder="tuliskan nama pembuat defect">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="where">Area Proses </label>
                    <input type="text" name="where" id="where" class="form-control" placeholder="Dimana Prosesnya?">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="why">Root Caused</label>
                    <input type="text" name="why" id="why" class="form-control" placeholder="Akar Permasalahannya">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="counter">Counter Measured</label>
                    <input type="text" name="counter" id="counter" class="form-control" placeholder="Action yang dilakukan">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="pic">PIC</label>
                    <input type="text" name="pic" id="pic" class="form-control" placeholder="PIC yang melakukan action">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="form-group">
                    <label for="due_date">DUE_DATE</label>
                    <input type="text" name="due_date" id="due_date" class="form-control" placeholder="Kapan Due Datenya?">
                    <div class="invalid-feedback"></div>
                </div>
                <div class="row form-group">
                    <button type="submit" class="btn btn-primary ml-3 tombol">Simpan</button>
                </div>
            </div>
        </div>
    </div>
</div>
<?= form_close() ?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
  $( function() {
    $( "#due_date" ).datepicker({
      dateFormat: "yy-mm-dd"
    });
  } );
</script>

Model

<?php

namespace App\Models;

use CodeIgniter\Model;

class ReportModel extends Model
{
    protected $table                = 'tbreport';
    protected $primaryKey           = 'id';
    protected $returnType           = 'App\Entities\Output';
    protected $allowedFields        = ['sisi', 'no', 'shift', 'assy', 'np', 'fourm', 'problem', 'jam', 'who', 'where', 'why', 'counter', 'pic', 'due_date'];

    public function getReport($problem = false)
    {
        $this->builder()->select('tbreport.*, tbdefect.nama_defect');
        if ($problem) {
            return $this->builder()->join('tbdefect', 'tbdefect.id = tbreport.problem')
                ->where(['problem' => $problem, 'aktif' => '1'])->orderBy('no ASC')->get();
        } else {
            return $this->builder()->join('tbdefect', 'tbdefect.id = tbreport.problem')
                ->orderBy('no ASC')->get();
        }
    }
}

image.png

image.png

avatar Faqih0776
@Faqih0776

4 Kontribusi 0 Poin

Diperbarui 2 tahun yang lalu

1 Jawaban:

<div>kalau multiple gunakan [], contoh :</div><pre> &lt;div class="form-group"&gt; &lt;label for="fourm"&gt;4M Change&lt;/label&gt;&lt;br&gt; &lt;input type="checkbox" name="fourm[]" id="fourm" value="Machine"&gt; Machine&lt;br&gt; &lt;input type="checkbox" name="fourm[]" id="fourm" value="Methode"&gt; Methode&lt;br&gt; &lt;input type="checkbox" name="fourm[]" id="fourm" value="Material"&gt; Material&lt;br&gt; &lt;input type="checkbox" name="fourm[]" id="fourm" value="Man"&gt; Man&lt;br&gt; &lt;div class="invalid-feedback"&gt;&lt;/div&gt; &lt;/div&gt; </pre><div><br>nnti dicontroller nya bisa di debug dulu buat ngecek sebelum disimpan</div><pre>dd($this-&gt;request-&gt;getVar('fourm')); // agar jadi string bukan array bisa gunakan implode implode(',',$this-&gt;request-&gt;getVar('fourm'));</pre>

avatar Terra
@Terra

81 Kontribusi 39 Poin

Dipost 2 tahun yang lalu

Tanggapan

Terima kasih ya atas insight nya, sudah work berdasarkan insight yang anda berikan

Login untuk ikut Jawaban