From Query | Laravel Excel


本站和网页 https://docs.laravel-excel.com/3.1/exports/from-query.html 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

From Query | Laravel Excel
Version LE 3.1 LE 3.0 LE 2.1 Nova 1.x
Commercial Support
Blog
(opens new window)
GitHub
(opens new window) Version LE 3.1 LE 3.0 LE 2.1 Nova 1.x
Commercial Support
Blog
(opens new window)
GitHub
(opens new window) Getting Started Architecture Concepts Exports 馃殌 5 minute quick startExporting collectionsStoring exports on diskExport formatsExportablesFrom QueryFrom ViewQueuedMultiple SheetsMapping dataCustomizing columnsSettingsDrawingsExport concernsExtendingTestingImports # From Query Customizing the queryAs constructor parameterAs setter In the previous example, we did the query inside the export class.
While this is a good solution for small exports,
for bigger exports this will come at a hefty performance price. By using the FromQuery concern, we can prepare a query for an export. Behind the scenes this query is executed in chunks. In the InvoicesExport class, add the FromQuery concern and return a query. Be sure to not ->get() the results! namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class InvoicesExport implements FromQuery
use Exportable;
public function query()
return Invoice::query();
123456789101112131415We can still download the export in the same way: return (new InvoicesExport)->download('invoices.xlsx');
1# Customizing the query It's easy to pass custom parameters to the query,
by simply passing them as dependencies to the export class. # As constructor parameter namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class InvoicesExport implements FromQuery
use Exportable;
public function __construct(int $year)
$this->year = $year;
public function query()
return Invoice::query()->whereYear('created_at', $this->year);
1234567891011121314151617181920The year can now be passed as dependency to the export class: return (new InvoicesExport(2018))->download('invoices.xlsx');
1# As setter namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class InvoicesExport implements FromQuery
use Exportable;
public function forYear(int $year)
$this->year = $year;
return $this;
public function query()
return Invoice::query()->whereYear('created_at', $this->year);
12345678910111213141516171819202122We can adjust the year by using the forYear method: return (new InvoicesExport)->forYear(2018)->download('invoices.xlsx');
1 Help us improve this page! (opens new window)
Exportables
From View