将当前搜索中的数据导出到Laravel中的excel中?

时间:2019-09-17 09:18:24

标签: excel laravel

搜索后,搜索结果显示在屏幕上。该内容我必须以excel格式下载。

Controller.php

public function searchList(Request $request)
{
    $fleet_issues = FleetIssue::all();
    $list = FleetIssue::orderBy('check_at', 'desc');
    $directory  = FleetManagementHelper :: getFleetIssueImageDirectory();
    $fleet_issue_type = FleetIssueType::all();
    $vehicle_class = VehicleClass::orderBy('id', 'asc')->get();
    $location= Location::orderBy('id', 'asc')->get();


         if( $request->plate_number){
             $list = $list->where('plate_number', $request->plate_number );
        }


        if( $request->branch){
            $list = $location->where('id', $request->branch );
        }

        if( $request->nric){
            $list = $list->where('nric',$request->nric);                       
        }

        if( $request->fleet_issue_type_id){
            $list = $list->where('fleet_issue_type_id',$request->fleet_issue_type_id);                       
        }

    $list = $list->paginate(20);
  view('pages.control_panel.view_issues',compact('list','fleet_issues','directory','fleet_issue_type','vehicle_class','location'));
}

下面是从Export.php下载Excel文件

public function downloadExcel()
{  
return Excel::download(new Export,'List.xlsx');
 }

下面是Export.php。如何在此处过滤搜索?

Export.php

Use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;


class Export implements FromView,ShouldAutoSize,WithHeadings, WithEvents
 {
  public function headings(): array
 {
     return [
         'Reservation Number', 'NRIC/Passport', 'Vehicle Model','Branch','Date of Checking','Issues'
     ];
 }

public function view() : View
{
    $vehicle_class = VehicleClass::orderBy('id', 'asc')->get();
    $location= Location::orderBy('id', 'asc')->get();
    $fleet_issue_type = FleetIssueType::orderBy('id', 'asc')->get();
    $list = FleetIssue::orderBy('check_at', 'desc')->paginate(20);
    $directory  = FleetManagementHelper :: getFleetIssueImageDirectory(); 

if(  $this->vehicle_model){
$list = $list->where('vehicle_model', $this->vehicle_model);
}
 return view('pages.control_panel.fleet_management.fleet_issues.list_fleet_issues',compact('vehicle_class','location','fleet_issue_type','list','directory'));

}

如何将搜索结果导出到excel?如何在Export中过滤搜索列表?如何像在控制器中那样在export中传递当前请求变量?

0 个答案:

没有答案