// Implement FlatfileFields to define your export fields (See later sections for details)
class ExportJob implements ShouldQueue, FlatfileFields
{
// FlatfileExporter magically find out, whether your auto-injecting method's class implement the FlatfileFields-interface!
// If so, it use this field definition by default
public function handle(FlatfileExport $flatfile, $exportFilepath = '/subfolderOnDisk/export.csv')
{
// Expose where to export the file. Based on file extension (ie. .csv) we select the proper exporter for you)
$flatfile->to($exportFilepath, 'diskNameOrInstance');
$flatfile->addHeader();
// You may want to load any data globally to prevent database queries for each row or even cell
$imagePaths = $this->imagepaths();
// Only needed for very custom contents in your flatfile!
$flatfile->beforeEachRow(function (Model $model) use ($imagePaths) {
// Do some very special magic to make custom image paths available for your "cells" for
// each row.
// Typically here you merge the globally loaded objects with the data you need for you cell
// $model here is an eloquent model selected by queryToSelectEachRow()
});
// Here we use a query builder (if you want to) and ensure to restrict memory usage by chunking
$this->queryToSelectEachRow()->chunk(500, function ($chunk) use ($flatfile) {
$flatfile->addRows($chunk);
});
// Dont forget to properly "close" the operation by this command
$flatfile->moveToTarget();
}
// In your field definition to are supposed to "only" pick out loaded or prepared data instead of
// doing complex calculations (See beforeEachRow())
public function fields(): array {
return []; // Your field definition
}
// Return an elequent query builder and carefully eager load relations you will gonna use in your cells!
protected function queryToSelectEachRow() {
return Products::whereCategory(15)->with('images');
}
}
// This will lookup for your field definition in the current class
class ExportJob implements ShouldQueue, FlatfileFields
{
public function handle(FlatfileExport $flatfile) {}
}
public function handle(FlatfileExport $flatfile) {
$flatfile->withFields($objImplementingFlatfileFields);
// Alternatively you can resolve and assign fields in one step
// app(FlatfileExport::class, [$objImplementingFlatfileFields]);
// app(FlatfileExport::class, ['fields' => $objImplementingFlatfileFields]);
}
$flatfile->to('relative/path/to/file-with-extension.csv', Storage::disk('name'));
// Do export ...
$flatfile->moveToTarget();
public function handle()
{
$flatfile = app(FlatfileExport::class, [$this]);
$flatfile->to($this->csvFilepath, $this->disk);
// Optionally add a Header
$flatfile->addHeader();
// Proposed way to step through a large result set
$this->queryToSelectEachRow()->chunk(500, function ($chunk) use ($flatfile) {
$flatfile->addRows($chunk);
});
}
protected function queryToSelectEachRow(): Builder
{
return CampaignModels::whereCampaignId($this->campaignId)->with(['model.product', 'campaign']);
}
$fields = [
'product_name' => 'Product Name'
];
$fields = [
'relation.columnOfRelation' => 'Column Header Label', // Relations should be eager loaded
[
'label' => 'Label with special characters',
'column' => 'relation.columnOfRelation', // Value of param $value in callback (optional)
'callback' => function ($model, $value) { // Format cell values
return $model->currencySign.' '.number_format($value);
}
],
'attribute' => [ // Column name can also still be the key of the array
'label' => 'Label with special characters',
'callback' => function ($model, $value) {}
],
'Column header' => function() { // For callbacks the header label can also be specified in the key! Crazy...
return 'static cell content';
},
'Empty cell' => '',
];
public function handle()
{
// ...
// relation has to be loaded in items of course
$items->each(function (Item $item) use ($export) {
$export->addRowForEachRelation($item, ['relation', 'more.*.relations'], 'fieldAccessorAlias', true);
});
// ...
}
public function fields(): array
{
return [
'fieldAccessorAlias.fieldName' => 'Output of relation fieldName if it is existing',
'fieldAccessorAlias.fieldNameMoreRelations' => 'Output of more.*.relations fieldName if it is existing',
];
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.