Search code examples
ajaxlaravelpdfphp-carbon

Data missing error when using Carbon::createFromFormat in laravel [dateFormat: year-month]


I'm getting "Data Missing" error when using Carbon to pass data to pdf view in Laravel. The query work when the data is pass back to the same page with dropdown list but getting error when pass to another page.

the error that i get enter image description here

I'm using date picker as my dropdown list & the date format that I'm using is: "2020-December"

ajax:

                changeMonth:true,
                changeYear:true,
                showButtonPanel:true,
                dateFormat: "yy-MM",
 
            onClose: function(dateText, inst) {  
                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); 
                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); 
                $(this).datepicker('setDate', new Date(year, month, 1)); 
              }
            });

 $('#convertBtn').click(function() {     
                var s = document.getElementById("sites");
                var site = s.options[s.selectedIndex].text;
                var startdate = $('#datepicker-8').val();

            if(site != '' && startdate  != '' )
            {  
                $.ajax({ 
                    paging: false,
                    searching: false, 
                    processing: true,
                    retrieve: true,
                    serverSide: true,
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    url:"{{ route('billing.bill') }}",
                    type:"GET",
                    dataType:"json",
                    data:{
                        site:site, 
                        startdate:startdate,
                        },
                    
                    success:function(data){
                     alert(JSON.stringify(data));
                     
                    },
                    error: function(data){
                        alert('Error');
                    }
                }) 
            } 
            else
            {
                alert("Please select 'Site' & 'Date'. ");
            }
        });

controller

$sites['report_date'] = Carbon::createFromFormat('Y-F', $request->startdate);
        $data = DB::table('monthly_data')
                ->where('site', $request->site)
                ->whereMonth('report_date', $sites['report_date'])
                ->whereYear('report_date',$sites['report_date'])
                ->get();

        $pdf = \App::make('dompdf.wrapper');
        $pdf -> loadView('pdf',compact('data'));
        $pdf -> setPaper('a4','landscape');
        return $pdf->stream('report.pdf');

may i know how to solve this error?


Solution

  • solution: do some changes in ajax

     
                    data: {
                        site: site,
                        startdate: startdate,
                    },
                    xhrFields: {
                        responseType: 'blob' // to avoid binary data being mangled on charset conversion
                    },
                    success: function(blob, status, xhr) {
    
                        var ieEDGE = navigator.userAgent.match('/Edge/g');
                        var ie = navigator.userAgent.match('/.NET/g'); // IE 11+
                        var oldIE = navigator.userAgent.match('/MSIE/g');
    
                        if (ie || oldIE || ieEDGE) {
                            window.navigator.msSaveBlob(blob, fileName);
                        } else {
                            var fileURL = URL.createObjectURL(blob);
                            document.write('<iframe src="' + fileURL + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
                        }
    
                    },
                    error: function(data) {
                        alert('Error');
                    }
    

    remove dataType:"json"; and replace it with xhrFields: { responseType: 'blob'} then it works