搜索后在Laravel中导出为PDF?
我现在拥有学校的搜索数据,我想将仅搜索到的数据导出为PDF.只需编写 $ school = School :: all()
即可轻松发送所有内容,但我不希望将所有仅搜索到的数据发送至PDF.有关更多信息,请参见所附图片.
I have search data of the school now I like to export the searched only data into PDF. It is easy to send all just write $school=School::all()
, but I like to send not all only searched data to PDF. For more information please see the attached pictures.
搜索后,我会显示以下页面:
Once I searched I get this page:
这是Laravel控制器中的代码.
Here is the code in Laravel controller.
搜索功能:
public function search(Request $request)
{
$camp_id=$request->camp_id;
$school_level=$request->school_level;
$school_shift=$request->school_shift;
$school_type=$request->school_type;
if($request->has('camp_id') && !empty($request->input('camp_id'))||$request->has('school_level') && !empty($request->input('school_level'))||$request->has('school_type') && !empty($request->input('school_type'))||$request->has('school_shift') && !empty($request->input('school_shift')))
$schools=School::where('camp_id',$camp_id)
->orWhere('school_level',$school_level)
->orWhere('school_type',$school_type)
->orWhere('school_shift',$school_shift)
->get();
else
$schools=School::where('school_active','Active');
return view('reports/search',compact('schools'));
}
这是导出为PDF的另一项功能:
public function exportpdf()
{
// if i write $schools=School::all(); it export to PDF which i dont like i want only searched as it show in attached pictuer 2
$pdf = PDF::loadView('reports.exportpdf',['schools'=>$schools])->setPaper('a3', 'landscape');
return $pdf->download('School Details Reports.pdf');
}
这是 web.php
代码:
Here is the web.php
code:
Route::get('reports/exportpdf','SchoolController@exportpdf')->name('exportpdf');
Route::post('reports/search','SchoolController@search')->name('search');
我的问题是如何创建 exportpdf()
函数,以及如何从视图中的按钮调用?
My question is how to make exportpdf()
function, and how to call from button in view?
首先使用post而不是获取路线
First use post instead of get for the route
Route::post('reports/exportpdf','SchoolController@exportpdf')->name('exportpdf');
<form action="{{ route('exportpdf') }}" method="post">
@csrf
@foreach($schools as $school)
<input type="hidden" name="school[]" value={{$school}}>
@endforeach
<button class="btn btn-info" type="submit">Export as PDF</button>
</form>
在您的控制器中,使用该模型
In your controller, use that model
public function exportpdf(Request $request)
{
// dd($request->school); // will provide an array
$schools = $request->school;
// then use it in your function
$pdf = PDF::loadView('reports.exportpdf',['schools'=>$schools])->setPaper('a3', 'landscape');
return $pdf->download('School Details Reports.pdf');
}