当日期“从”和“到”为空时,Laravel始终在我的报告中显示1970年1月1日/ 1970年1月1日

时间:2020-05-12 16:10:25

标签: laravel date datetime if-statement report

当日期为空时,我尝试使报告显示“全部”而不是默认日期值“ {{1970年1月1日/ 1970年1月1日}”,我在这里已经阅读了其他类似案例并尝试过解决方案,但对我不起作用 我在这里使用date而不是datetime,而我的$ from和$ to只是一个字段,而不是数据库中列的一部分,我只是使用它来过滤我的报表,

StockinController.php

public function print(Request $req)
    {
      $method = $req->method();

        if ($req->isMethod('post'))
        {
            $from = $req->input('from');
            $to   = $req->input('to');

            $start_date = date('d-M-Y', strtotime($from));
            $end_date = date('d-M-Y', strtotime($to));

            if ($req->has('search'))
            {
                if ($from && $to != null) {
                $search = \App\Stockin::whereBetween('tanggal',[$from,$to])->get();
                return view('transaction.stockin.stockinform',['data_stockin' => $search]);
                } 
                else
                {
                $data_stockin = \App\Stockin::all();
                return view('transaction.stockin.stockinform',['data_stockin' => $data_stockin]);
                }

            } 
            elseif ($req->has('exportPDF'))
            { 
                if ($from && $to != null) {
                $PDFReport = \App\Stockin::whereBetween('tanggal',[$from,$to])->get();
                $pdf = PDF::loadView('transaction.stockin.stockin_pdf', ['data_stockin' => $PDFReport,'from' => $start_date, 'to' => $end_date])->setPaper('a4');
                  return $pdf->stream();
                }
                else
                {
                  $PDFReport =  \App\Stockin::all();
                  $pdf = PDF::loadView('transaction.stockin.stockin_pdf', ['data_stockin' => $PDFReport,'from' => $start_date, 'to' => $end_date])->setPaper('a4');
                  return $pdf->stream();
                }
            }  
        }
    } 

stockin_pdf.blade.php

<center>
    <h5>STOCK-IN REPORT</h5>
    <hr>
    @if($from && $to != null) {
        <small>{{$from}}  /  {{$to}} </small>
    }
    @else {
        <small>All</small>
    }
    @endif 
</center>

预先感谢您:)

1 个答案:

答案 0 :(得分:1)

您要获取日期01-Jan-1970,因为传递给date()的字符串是空字符串或null

尝试这样的事情:

$start_date = !empty($from) ? date('d-M-Y', strtotime($from)) : null;
$end_date = !empty($to) ? date('d-M-Y', strtotime($to)) : null;