noblemfd
2 weeks ago
135
3
General

How to pass Holiday Dates from controller in View in Laravel using JSON

Posted 2 weeks ago by noblemfd

I am using JQuery-UI datepicker in Laravel-5.8

Controller:

    public function findNationalHoliday(Request $request)
    {
       $nationalholidays               = HrHolidayDate::select('holiday_date')where('company_id', $userCompany)->whereYear('created_at', '=', date('Y'))->get();
     return response()->json([
        'nationalholidays' => $nationalholidays,
     ]);        
   } 

View

        <script type="text/javascript">
        $(document).ready(function() {
            $(document).on('change', '#leave_type', function() {
                var air_id =  $(this).val();
                var a = $(this).parent();
                var op = "";
                
                $.ajax({
                    type: 'get',
                    url: '{{ route('get.enationalholidays.all') }}',
                    data: { 'id': air_id },
                    dataType: 'json',      //return data will be json
                    success: function(data) {
                        $('#nationalholidays').val(data.nationalholidays);
                    },
                    error:function(){

                    }
                });
            });
        });
        </script>

    <script type="text/javascript">
           $("#leave_days").on('keyup blur', function (e) {
            var periodval=parseInt($("#leave_days").val());

            var startDate = $('.start_date');
            var endDate = $('.end_date');

            var dte = startDate.datepicker("getDate"); 
                    dte.setDate(dte.getDate()+periodval); 
                    endDate.datepicker("setDate", dte);            

          });

            $( '.start_date' ).datepicker({
                dateFormat: 'dd-mm-yy',
                changeMonth: true,
                changeYear: true,      
                showAnim: 'slideDown',
                duration: 'fast',   
                minDate: +1,
                setDate: new Date(),
                beforeShowDay: $.datepicker.noWeekends,
                yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
        }).datepicker('setDate', '1');

            $( '.end_date' ).datepicker({
                dateFormat: 'dd-mm-yy',
                changeMonth: true,
                changeYear: true,      
                showAnim: 'slideDown',
                duration: 'fast',   
                minDate: +1,
                beforeShowDay: $.datepicker.noWeekends,
                yearRange: new Date().getFullYear() + ':' + new Date().getFullYear(),
                enableOnReadonly: true,
                beforeShow: function(i) { if ($(i).attr('readonly')) { return false; } }
             });
        });
     </script>

In the code, I have leave_days, start_date and end_date. end_date is disabled. While leave_days on keyup, I add leave_days to start_date to get end_date.

But I also have a model called HrHolidayDate which has a field, holiday_date in date format. I use json in controller to send it to view.

How do I consume the holiday_date in the view and remove it from my result in end_date calculation?

Thanks

Please sign in or create an account to participate in this conversation.