问题
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.
all Schools and it information
Once I searched I get this page:
searched page
Here is the code in Laravel controller.
Search function:
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'));
}
Here is another function which export to 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');
}
Here is the web.php
code:
Route::get('reports/exportpdf','SchoolController@exportpdf')->name('exportpdf');
Route::post('reports/search','SchoolController@search')->name('search');
My question is how to make exportpdf()
function, and how to call from button in view?
回答1:
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');
}
来源:https://stackoverflow.com/questions/65167212/after-search-export-to-pdf-in-laravel