Exportables

In the previous example, we used the Simplesheet::download facade to start an export.

Laravel Simplesheet also provides a Nikazooz\Simplesheet\Concerns\Exportable trait, to make export classes exportable.

namespace App\Exports;

use App\Invoice;
use Nikazooz\Simplesheet\Concerns\Exportable;
use Nikazooz\Simplesheet\Concerns\FromCollection;

class InvoicesExport implements FromCollection
{
    use Exportable;

    public function collection()
    {
        return Invoice::all();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

We can now download the export without the need for the facade:

return (new InvoicesExport)->download('invoices.xlsx');
1

Or store it on a disk:

return (new InvoicesExport)->store('invoices.xlsx', 's3');
1

You can also pass options to the disk if you like:

return (new InvoicesExport)->store('invoices.xlsx', 's3', null, 'private');
1

Responsable

The previous example can be made even shorter when adding Laravel's Responsable interface to the export class:

namespace App\Exports;

use App\Invoice;
use Illuminate\Contracts\Support\Responsable;
use Nikazooz\Simplesheet\Concerns\Exportable;
use Nikazooz\Simplesheet\Concerns\FromCollection;

class InvoicesExport implements FromCollection, Responsable
{
    use Exportable;

    /**
    * It's required to define the fileName within
    * the export class when making use of Responsable.
    */
    private $fileName = 'invoices.xlsx';

    public function collection()
    {
        return Invoice::all();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

You can now easily return the export class, without the need of calling ->download().

return new InvoicesExport();
1