0

I'm using Laravel 5.8 and I tried creating a custom command like this:

php artisan make:command app exportappresults

And the Command goes like this:

protected $signature = 'app:exportappresults';

protected $description = 'Export App Exam Result Into Excel';

public function __construct()
{
    parent::__construct();
}

public function handle()
{
    return Excel::download(new TavanmandAppUserExamExport,'userexamlist.xlsx');
}

So as you can see I have used Laravel Excel and tried exporting data into Excel file.

Note that this code works fine in the Controller and can properly export an Excel file.

But now I don't know where does the exported excel file from the DB goes when I use the Console Command.

So if you know, please let me know...

Thanks.

Pouya
  • 114
  • 1
  • 8
  • 36

1 Answers1

1

Excel::download is used in HTTP controllers, to pass exported data to HTTP response. If you need to store file on disk, use Excel::store instead:

public function handle()
{
    Excel::store(new TavanmandAppUserExamExport, 'userexamlist.xlsx');
}

File will be stored in default storage (disk), configured in laravel's config/filesystems.php, by default it's ./storage/app.

Also you may specify another disk as third argument of Excel::store method, see Storing exports on disk

Catzilla
  • 312
  • 1
  • 5
  • I tried that but didn't find any `userexamlist.xlsx` file in `storage/app` directory – Pouya Sep 06 '22 at 07:53
  • Check write permissions of `storage/app`, and errors in laravel log. Also check which storage driver is used (variable `FILESYSTEM_DRIVER` in `.env`, and storage paths in `config/filesystems.php`). Also try to explicitly set `local` as third argument of `Excel::store` – Catzilla Sep 06 '22 at 08:10