Exporting collections

The easiest way to start an export is to create a custom export class. We'll use an invoices export as example.

Create a new class called InvoicesExport in App/Exports:

namespace App\Exports;

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

class InvoicesExport implements FromCollection
{
    public function collection()
    {
        return Invoice::all();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12

In your controller we can now download this export:

public function export()
{
    return Simplesheet::download(new InvoicesExport, 'invoices.xlsx');
}
1
2
3
4

Or store it on a (e.g. s3) disk:

public function storeSheet()
{
    return Simplesheet::store(new InvoicesExport, 'invoices.xlsx', 's3');
}
1
2
3
4

💡 More about storing exports can be found in the storing exports on disk page.

TIP

If you want to use relationships in Collection, combine with Mapping Data

Using custom structures

If you are not using Eloquent or having another datasource (e.g. an API, MongoDB, Cache, ...) you can also return a custom collection:

namespace App\Exports;

use App\Invoice;
use Illuminate\Support\Collection;
use Nikazooz\Simplesheet\Concerns\FromCollection;

class InvoicesExport implements FromCollection
{
    public function collection()
    {
        return new Collection([
            [1, 2, 3],
            [4, 5, 6]
        ]);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Using arrays

If you prefer to use plain arrays over Collections, you can use the FromArray concern:

namespace App\Exports;

use App\Invoice;
use Nikazooz\Simplesheet\Concerns\FromArray;

class InvoicesExport implements FromArray
{
    public function array(): array
    {
        return [
            [1, 2, 3],
            [4, 5, 6]
        ];
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

If you need to pass data from the controller to your export, you can use the constructor to do so:

namespace App\Exports;

use App\Invoice;
use Nikazooz\Simplesheet\Concerns\FromArray;

class InvoicesExport implements FromArray
{
    protected $invoices;

    public function __construct(array $invoices)
    {
        $this->invoices = $invoices;
    }

    public function array(): array
    {
        return $this->invoices;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

In your controller you can now use the constructor of the export class:

public function export()
{
    $export = new InvoicesExport([
        [1, 2, 3],
        [4, 5, 6]
    ]);

    return Simplesheet::download($export, 'invoices.xlsx');
}
1
2
3
4
5
6
7
8
9

Dependency injection

In case your export needs dependencies, you can inject the export class:

namespace App\Exports;

use Nikazooz\Simplesheet\Concerns\FromCollection;

class InvoicesExport implements FromCollection
{
    public function __construct(InvoicesRepository $invoices)
    {
        $this->invoices = $invoices;
    }

    public function collection()
    {
        return $this->invoices->all();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public function export(Simplesheet $simplesheet, InvoicesExport $export)
{
    return $simplesheet->download($export, 'invoices.xlsx');
}
1
2
3
4

Storing raw contents

If you want to receive the raw contents of the exported file, you can use the raw() method:

$contents = Simplesheet::raw(new InvoicesExport, Simplesheet::XLSX);
1