# Data sources
An important part of each export is where the data should come from. Laravel Excel offers several ways of feeding data to the spreadsheet.
# TLDR;
1 Exporting from array or collection
Using the FromCollection
and FromArray
concerns any type list of rows can be exported.
2 Exporting a Model query
Models can be exported using the FromQuery
concern. Behind the scenes the query will be chunked for improved
performance.
3 Exporting a Blade View
To have more control on how the export should be presented FromView
can be used with a Blade view that has a HTML
table.
# Collection
# Eloquent collections
FromCollection
allows a collection of Eloquent models to be returned. Every Model will become a row in the export.
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
public function collection()
{
// All User models.
return User::all();
// Query some User models.
return User::query()->select('')->where('country', 'NL')->get();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
You can also do a more complex query with select, joins, where conditions etc. Just like with any Export object, you can pass any data via the constructor (or setters).
class UsersExport implements FromCollection
{
public function __construct(
public string $country
) {
}
public function collection()
{
return User::query()
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.name', 'users.country', 'orders.price')
->where('users.country', $this->country)
->get();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
In the controller you can pass any required data to the export.
return Excel::download(new UsersExport('NL'));
# Custom collections
If you are not using Eloquent or having another datasource (e.g. an API, MongoDB, Cache, ...) you can also return a custom collection:
class InvoicesExport implements FromCollection
{
public function collection()
{
return new Collection([
[1, 2, 3],
[4, 5, 6]
]);
}
}
2
3
4
5
6
7
8
9
10
# Lazy Collection
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
return Invoice::query()->lazy();
}
}
2
3
4
5
6
7
8
9
10
11
12
# Array
If you prefer to use plain arrays over Collections, you can use the FromArray concern:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromArray;
class InvoicesExport implements FromArray
{
public function array(): array
{
return [
[1, 2, 3],
[4, 5, 6]
];
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Query
In the previous examples, we did the query inside the export class. While this is a good solution for small exports, for bigger exports this will come at a hefty performance price.
By using the FromQuery
concern, we can prepare a query for an export. Behind the scenes this query is executed in
chunks to limit the amount of models held in memory.
In the InvoicesExport
class, add the FromQuery
concern and return a query. Be sure to not ->get()
the results!
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
class InvoicesExport implements FromQuery
{
use Exportable;
public function query()
{
return Invoice::query();
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# View
Exports can be created from a Blade view, by using the FromView concern.
namespace App\Exports;
use App\User;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
class UsersExport implements FromView
{
public function view(): View
{
return view('exports.users', [
'users' => User::all()
]);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
It will convert an HTML table into an Excel spreadsheet. For example; exports/users.blade.php
:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Generators
Exports can be created from a PHP generator class, by using the FromGenerator
concern.
A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array in memory.
namespace App\Exports;
use Generator;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromGenerator;
class DataExport implements FromGenerator
{
public function generator(): Generator
{
for ($i = 1; $i <= 100; $i++) {
yield [$i, $i+1, $i+2];
}
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Generic data manipulations
# Strict null comparison
If you want your 0
values to be actual 0
values in your Excel sheet instead of null
(empty cells), you can use WithStrictNullComparison
.
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
class UsersExport implements FromCollection, WithStrictNullComparison
2
3
# Custom start cell
The default start cell is A1. Implementing the WithCustomStartCell
concern in your export class allows you to specify a custom start cell.
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
class UsersExport implements FromCollection, WithCustomStartCell
{
public function startCell(): string
{
return 'B2';
}
}
2
3
4
5
6
7
8
9
WithCustomStartCell is only supported for FromCollection exports.