fileName; } /** * @return bool */ public function run(): bool { // create temporary file: $this->tempFile(); // necessary for CSV writer: $fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName; // create CSV writer: $writer = Writer::createFromPath(new SplFileObject($fullPath, 'a+'), 'w'); // all rows: $rows = []; // add header: $rows[] = array_keys(Entry::getFieldsAndTypes()); // then the rest: /** @var Entry $entry */ foreach ($this->getEntries() as $entry) { // order is defined in Entry::getFieldsAndTypes. $rows[] = [ $entry->description, $entry->amount, $entry->date, $entry->sourceAccount->id, $entry->sourceAccount->name, $entry->sourceAccount->iban, $entry->sourceAccount->type, $entry->sourceAccount->number, $entry->destinationAccount->id, $entry->destinationAccount->name, $entry->destinationAccount->iban, $entry->destinationAccount->type, $entry->destinationAccount->number, $entry->budget->id, $entry->budget->name, $entry->category->id, $entry->category->name, $entry->bill->id, $entry->bill->name, ]; } $writer->insertAll($rows); return true; } private function tempFile() { $this->fileName = $this->job->key . '-records.csv'; } }