i use laravel 8 and maatwebsite 3.1 , want to download (export) a table of my database as excel file by clicking an icon . as i searched , we must create a class by artisan php artisan make:export blogExport --model=blog
and in controller use download method . Excel::download(new blogExport, 'filename.xlsx');
. blogExport include collection() function that we should determine output matter in it .But I want write function in controller and determine the output i want , directly in this function and not use blogExport . is it possible ? there is a method "CREATE" that can use but it is old and not supported by maatwebsite 3.1 . any reply is appreciated .
CodePudding user response:
class ExcellExport implements FromCollection, WithHeadings, WithEvents, WithStrictNullComparison,ShouldAutoSize
{
use Exportable;
public function __construct($header, $query)
{
$this->header = $header;
$this->query = $query;
}
//======================================
public function headings(): array
{
return [$this->header];
}
//======================================
public function collection()
{
return collect($this->query);
}
}
then pass your query like this:
$query = Estate::where($inputs)->with('landlord', 'rented', 'my_estate_list');
$headers=["code,type_caption,space,landlord,area,address,price"];
$resultExcell = new ExcellExport($headers, $query);
return Excel::download($resultExcell, 'file_name.xlsx');