0

I have a class that exports a excel file as a scheduled job:

class exportExcelBasedOnID {
  public function __construct(ScheduleJob $scheduleJob) {
    $scheduleJob->processed_at = Carbon::now();
        $scheduleJob->processing = true;
        $scheduleJob->exception = null;
        $scheduleJob->save();

        $this->exportExcels($scheduleJob);
    }

  private function exportExcels(ScheduleJob $scheduleJob) {

    $school = $scheduleJob->school;
        $payload = unserialize($scheduleJob->payload);
        $id = $payload['id'];
        $requestUser = User::where('id', $scheduleJob->user_id)->first();
        $students = Excels::where('id', $id);
        $locale = array_key_exists('locale', $payload) ? $payload['locale'] : \App::getLocale();

//a bunch of code to create the file with format called $final

    $final->store('xlsx', storage_path('app/public/EXCEL/'.$school->school_code));
        
        $payload = unserialize($scheduleJob->payload);
        $payload['payload'] = $name.'.xlsx';
        $scheduleJob->payload = serialize($payload);
        $scheduleJob->processed_at = Carbon::now();
        $scheduleJob->exception = null;
        $scheduleJob->processing = false;
        $scheduleJob->save();
    }
}

How do I connect it to the following so instead of the excel file being scheduled to download, the excel is stored in a folder called students and zipped and then be scheduled to download afterwards?

My zip code:

public function export_schedule_zip(School $school, Request $request)
    {
        $zip = new ZipArchive;
        $zipname = 'students.zip';
        $zip_folder = 'student';
        $zip->open($zipname, ZipArchive::CREATE);
        $zip->addEmptyDir($zip_folder);

        $zip->close();
        return response()->download($zipname);

    }


apkr x
  • 11
  • 3
  • Shouldn't you `$scheduleJob->save($path);` ? – IT goldman Jun 23 '22 at 07:29
  • @ITgoldman it is able to save accurately like this. But my issue is I'm not sure how to save it in the zip file instead. – apkr x Jun 23 '22 at 07:43
  • You need to ZIP it yourself afterwards: https://www.php.net/manual/en/zip.examples.php. Then you need to download it yourself: https://www.php.net/manual/en/function.readfile.php – IT goldman Jun 23 '22 at 07:57

0 Answers0