suchen

Heim  >  Fragen und Antworten  >  Hauptteil

LARAVEL 9: Daten im Formularschema nicht erfolgreich in der Datenbank gespeichert, wenn id_jenisizin-Wert == 1 ||

Ich erstelle ein Mitarbeiterurlaubsformular, das drei Arten von Berechtigungen aus einer anderen Tabelle enthält, nämlich: „Terlambat“, „Pulang Cepat“, „Sakit“. Wählen Sie Kategorieoptionen

Für die Kategorie „SAKIT“ wurden die Daten erfolgreich in der Datenbank gespeichert, da keine jam_mulai und jam_selesai ausgefüllt werden müssen. Formular der Kategorie „SAKIT“

Für die Kategorien „Terlambat“, „Pulang Cepat“. Wenn ausgewählt, werden die Formulare jam_mulai und jam_selesai angezeigt. Und das jml_jam-Feld, das ich im Controller erstellt habe. Aber beim Absenden erscheinen die Daten meines DD ($IZIN) nicht, sondern kehren zur Seite index.blade.php zurück. Formulare für „LATE“ und „HOME QUICK“

Dies ist die Antwort der Netzwerkseite bei der Überprüfung: Vorschau Netzwerk-Header

Dies ist der Code zum Hinzufügen von Datenberechtigungen im Formularmodus:

enter code here    {{-- FORM PENGAJUAN IZIN--}}
{{-- bbootsrapt clockpicker  --}}
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/clockpicker/0.0.7/bootstrap-clockpicker.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/clockpicker/0.0.7/bootstrap-clockpicker.min.js"></script>

<div class="modal fade" id="smallModal" tabindex="-1" role="dialog" aria-labelledby="smallModal" aria-hidden="true">
    <div class="modal-dialog modal-md">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">Form Permohonan Izin</h4>
            </div>
                   
            <div class="modal-body">
                <form class="input" action="{{ route('izinstore')}}" method="POST" enctype="multipart/form-data">
                    @csrf
                    @method('POST')
                    <div class="form-group">
                        <label for="id_karyawan" class="col-form-label">Nama</label>
                        <input type="text" class="form-control" id="id_karyawan" value="{{Auth::user()->name}}" readonly>
                        <input type="hidden" class="form-control" name="id_karyawan" id="id_karyawan" value="{{$karyawan}}" hidden>
                    </div>
                    <div class="form-group col-sm" id="jenisizin">
                        <label for="id_jenisizin" class="col-form-label">Kategori Izin</label>
                        <select name="id_jenisizin" id="id_jenisizin" class="form-control">
                            <option>-- Pilih Kategori --</option>
                            @foreach ($jenisizin as $data)
                                <option value="{{ $data->id}}" 
                                    {{ old('id_jenisizin') == $data->id ? 'selected' : '' }}
                                    >{{ $data->jenis_izin }}
                                </option>
                            @endforeach
                        </select> 
                    </div>
                    <div class="form-group">
                        <label for="keperluan" class="col-form-label">Keperluan</label>
                        <input type="text" class="form-control" name="keperluan" id="keperluan" required>
                    </div>
                    <div class="row">
                        <div class="col-sm-6">
                            <div class="">
                                {{-- <form class="" action="#"> --}}
                                    <div class="form-group">
                                        <label for="tgl_mulai" class="form-label">Tanggal Mulai</label>
                                        <div class="input-group">
                                            <input type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose3" name="tgl_mulai" onchange=(jumlahhari()) autocomplete="off" required>
                                            <span class="input-group-addon bg-custom b-0"><i class="mdi mdi-calendar text-white"></i></span>
                                        </div>
                                    </div>
                                {{-- </form> --}}
                            </div>
                        </div>
                       
                        <div class="col-sm-6">
                            <div class="">
                                {{-- <form class="" action="#"> --}}
                                    <div class="form-group">
                                        <label for="tgl_selesai" class="form-label">Tanggal Selesai</label>
                                        <div class="input-group">
                                            <input type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose4" name="tgl_selesai"  onchange=(jumlahhari()) autocomplete="off" required>
                                            <span class="input-group-addon bg-custom b-0"><i class="mdi mdi-calendar text-white"></i></span>
                                        </div>
                                    </div>
                                {{-- </form> --}}
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-6" id="jmulai">  
                            <div class="">
                                <label for="jam_mulai">Dari Jam</label>
                                <div class="input-group clockpicker pull-center" data-placement="top" data-align="top" autocomplete="off" data-autoclose="true">
                                    <input type="text" class="form-control" name="jam_mulai" id="mulai" value="{{ old('jam_mulai') }}">
                                    <span class="input-group-addon">
                                        <span class="fa fa-clock-o"></span>
                                    </span>
                                </div>
                            </div>
                        </div>
                               
                        <div class="col-lg-6" id="jselesai">
                            <div class="">
                                <label for="jam_selesai">Sampai Jam</label>
                                <div class="input-group clockpicker pull-center" data-placement="top" data-align="top" autocomplete="off" data-autoclose="true">
                                    <input type="text" class="form-control" name="jam_selesai" id="selesai" value="{{ old('jam_selesai') }}">
                                    <span class="input-group-addon">
                                        <span class="fa fa-clock-o"></span>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="form-group col-sm" id="jumlahhari">
                        <label for="jml_hari" class="col-form-label">Jumlah Hari</label>
                        <input type="text" class="form-control" name="jml_hari" id="jml" readonly>
                    </div>


                    <div class="form-group col-sm"  id="jumlahjam">
                        {{-- <label for="jml_jam" class="col-form-label">Jumlah Jam</label> --}}
                        <input type="hidden" class="form-control" name="jml_jam" value="{{old('jml_jam')}}" id="jam">
                    </div>

                    <div class="form-group col-sm">
                        <input type="hidden" class="form-control" name="status" id="status" value="Pending" hidden>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-success" value="save">Send</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
    
<script type="text/javascript">
//show clockpicker 
    jQuery(function(j){
        j('.clockpicker').clockpicker()
            .find('input').change(function()
            {
                console.log(this.value);
            });
        var input = j('#single-input').clockpicker({
            placement: 'bottom',
            align: 'left',
            autoclose: true,
            'default': 'now'
        });
    });

//show/hide clockpicker when id_jenisizin selected
    $(function()
    {
        $('#jmulai').prop("hidden", true);
        $('#jselesai').prop("hidden", true);
        // $('#jumlahjam').prop("hidden", true);
        $('#jumlahhari').prop("hidden", true);

        $('#jenisizin').on('change', function(e)
        {
            if(e.target.value== 1 || e.target.value== 2)
            {
                $('#jmulai').prop("hidden", false);
                $('#jselesai').prop("hidden", false);
                // $('#jumlahjam').prop("hidden", false);
                $('#jumlahhari').prop("hidden", false);

                //selisih waktu jika terlambat
                if(e.target.value== 1)
                {
                    //set nilai jam_mulai
                    $('#mulai').val('08:00');
                    $('#mulai').attr('readonly', false);
                    $('#mulai').css('background-color' , '#DEDEDE');
                }else {

                    //set nilai jam_selesai
                    $('#selesai').val('17:00');
                    $('#selesai').attr('readonly', false);
                    $('#selesai').css('background-color' , '#DEDEDE');
                }
                // alert('DATA ADA');
            } else {
                $('#jmulai').prop("hidden", true);
                $('#jselesai').prop("hidden", true);
                // $('#jumlahjam').prop("hidden", true);
                $('#jumlahhari').prop("hidden", false);
            }
            // alert('id:' + e.target.value);
        });
    });

// =========================================================
//datepicker for tgl_mulai & tgl_selesai
    $('#datepicker-autoclose3').datepicker({
        autoclose: true,
    });

    $('#datepicker-autoclose4').datepicker({
        autoclose: true,
    });

    function jumlahhari(){
        var start= $('#datepicker-autoclose3').val();
        var end  = $('#datepicker-autoclose4').val();

        var start_date= new Date(start);
        var end_date  = new Date(end) ;
        var daysOfYear= [];

        //mendapatkan jumlah hari izin jika sakit
        for (var d = start_date; d <= end_date; d.setDate(d.getDate() + 1)){
            //cek workdays
            let tgl = new Date(d);
            if(tgl.getDay() !=0 && tgl.getDay() !=6){
                daysOfYear.push(tgl);
                console.log(tgl);
            } else{
                console.log("hari Libur" + tgl.getDay());
            };
        };

        //mengambil value tanggal mulai
        $('#start_date').on('change', function(){
            jumlahhari();
        });
        //mengambil value tanggal selesai
        $('#end_date').on('change', function(){
            jumlahhari();
        });

        console.info(daysOfYear);
        $('#jml').val(daysOfYear.length ?? 0);
    };

</script>


<!-- jQuery  -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
<script src="assets/js/modernizr.min.js"></script>
<script src="assets/js/detect.js"></script>
<script src="assets/js/fastclick.js"></script>
<script src="assets/js/jquery.slimscroll.js"></script>
<script src="assets/js/jquery.blockUI.js"></script>
<script src="assets/js/waves.js"></script>
<script src="assets/js/wow.min.js"></script>
<script src="assets/js/jquery.nicescroll.js"></script>
<script src="assets/js/jquery.scrollTo.min.js"></script>

{{--  plugin js --}}
<script src="assets/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>


{{-- // Datatable init js  --}}
<script src="assets/pages/datatables.init.js"></script>
<script src="assets/js/app.js"></script>

{{-- // Plugins Init js --}}
<script src="assets/pages/form-advanced.js"></script>

Dies ist der Code im CONTROLLER zum Speichern von Daten:

public function store(Request $request)
{
    $karyawan = Auth::user()->karyawans->id;
    // dd($request->id_jenisizin);
    if($request->has('jam_mulai') && $request->has('jam_selesai'))
    {
        if($request->id_jenisizin == 1 || $request->id_jenisizin == 2)
        {
            // dd($request->all());
            $validate = $request->validate([
                'id_karyawan'  => 'required',
                'id_jenisizin' => 'required',
                'keperluan'    => 'required',
                'tgl_mulai'    => 'required',
                'tgl_selesai'  => 'required',
                'jam_mulai'    => 'required',
                'jam_selesai'  => 'required',
                'jml_hari'     => 'required',
                'jml_jam'      => 'required',
                'status'       => 'required',
            ]);
            // dd($validate);

            $izin = New Izin;
            $izin->id_karyawan = $karyawan;
            $izin->id_jenisizin= $request->id_jenisizin;
            $izin->keperluan   = $request->keperluan;
            $izin->tgl_mulai   = Carbon::now()->format("Y-m-d");
            $izin->tgl_selesai = Carbon::now()->format("Y-m-d");
            $izin->jam_mulai   = $request->jam_mulai;
            $izin->jam_selesai = $request->jam_selesai;
            $izin->jml_hari    = $request->jml_hari;

            $jammulai  = Carbon::parse($request->jam_mulai);
            $jamselesai= Carbon::parse($request->jam_selesai);
            $time_range= $jamselesai->diff($jammulai)->format("%H:%I");

            $izin->jml_jam     = $time_range;
            $izin->status      = 'Pending';

            $izin->save();

            dd($izin);
        
            return redirect()->back()->withInput();
        }else{
            // dd($request->all());

            $validate = $request->validate([
                'id_karyawan'  => 'required',
                'id_jenisizin' => 'required',
                'keperluan'    => 'required',
                'tgl_mulai'    => 'required',
                'tgl_selesai'  => 'required',
                'jml_hari'     => 'required',
                'status'       => 'required',
            ]);

            // dd($validate);

            $izin = New Izin;
            $izin->id_karyawan = $karyawan;
            $izin->id_jenisizin= $request->id_jenisizin;
            $izin->keperluan   = $request->keperluan;
            $izin->tgl_mulai   = Carbon::now()->format("Y-m-d");
            $izin->tgl_selesai = Carbon::now()->format("Y-m-d");
            $izin->jml_hari    = $request->jml_hari;
            $izin->status      = 'Pending';

            $izin->save();
            // dd($izin);
           
            return redirect()->back()->withInput();
        };
    }
        
}

Dies ist die PERMISSION-Tabelle in der Laragon-Datenbank: Tabelle IZIN

in der Datenbank
P粉781235689P粉781235689307 Tage vor368

Antworte allen(1)Ich werde antworten

  • P粉587970021

    P粉5879700212024-02-26 09:42:16

    您可以尝试在blade文件中打印错误,因为下面的代码可能会帮助您。

    控制器代码

    if($validator->fails()) {
        return Redirect::back()->withErrors($validator);
    }

    在刀片文件中

    @if($errors->any())
        {{ implode('', $errors->all('<div>:message</div>')) }}
    @endif

    Antwort
    0
  • StornierenAntwort