laravel有很多輔助函數,如view() 函數擷取一個視圖實列
判斷視圖是否存在的方法就是view()->exists()方法,如果存在的話傳回true,不存在則傳回false
以下是部分代碼片段案例
$data->load(['subjects' => function ($query) {
$query->with('category');
}]);
$tpl = 'pdf/' . strtolower(substr($data->subjects->code, 0, 1));
if (!view()->exists($tpl)) {
$tpl = 'pdf/pdf';
}
$pdf = \PDF::loadView($tpl, $data);
return $pdf->download('報名表.pdf');