fileName; } /** * @return bool */ public function run(): bool { // create temporary file: $this->tempFile(); // necessary for CSV writer: $fullPath = storage_path('export') . DIRECTORY_SEPARATOR . $this->fileName; $writer = Writer::createFromPath(new SplFileObject($fullPath, 'a+'), 'w'); $rows = []; // get field names for header row: $first = $this->getEntries()->first(); $headers = array_keys(get_object_vars($first)); $rows[] = $headers; /** @var Entry $entry */ foreach ($this->getEntries() as $entry) { $line = []; foreach ($headers as $header) { $line[] = $entry->$header; } $rows[] = $line; } $writer->insertAll($rows); return true; } private function tempFile() { $this->fileName = $this->job->key . '-records.csv'; } }