Skip to content

Commit 8f9870f

Browse files
Update docs
1 parent 64521f6 commit 8f9870f

File tree

10 files changed

+220
-29
lines changed

10 files changed

+220
-29
lines changed

.vuepress/theme/styles/code.styl

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
background-color rgb(227 220 241)
2121
border-radius 16px
2222
font-weight bold
23+
white-space nowrap
2324

2425
.content__default
2526
pre, pre[class*="language-"]

4.x/exports/columns.md

+6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ If no `attribute` is passed, the title will be converted to a snake case attribu
108108
Text::make('Name'); // uses "name" attribute
109109
```
110110

111+
When working with relationships (e.g. HasOne and BelongsTo), you can use a dot notation as attribute:
112+
113+
```php
114+
Text::make('Office', 'office.name'); // uses "name" attribute of the "office" BelongsTo relationship.
115+
```
116+
111117
A callback can also be passed as second argument. The closure will get the row or Model. Whatever is returned within the
112118
closure, will be inserted in the cell.
113119

4.x/exports/concern-overview.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Concern overview

4.x/exports/data.md

+85-14
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Data sources
22

3-
An important part of each export is where the data should come from. Laravel Excel offers several ways of
4-
feeding data to the spreadsheet.
3+
An important part of each export is where the data should come from. Laravel Excel offers several ways of feeding data
4+
to the spreadsheet.
55

66
[[toc]]
77

@@ -13,34 +13,70 @@ Using the `FromCollection` and `FromArray` concerns any type list of rows can be
1313

1414
<span class="inline-step">2</span> **Exporting a Model query**
1515

16-
Models can be exported using the `FromQuery` concern. Behind the scenes the query will be chunked for improved performance.
16+
Models can be exported using the `FromQuery` concern. Behind the scenes the query will be chunked for improved
17+
performance.
1718

1819
<span class="inline-step">3</span> **Exporting a Blade View**
1920

20-
To have more control on how the export should be presented `FromView` can be used with a Blade view that has a HTML table.
21+
To have more control on how the export should be presented `FromView` can be used with a Blade view that has a HTML
22+
table.
2123

2224
## Collection
2325

2426
### Eloquent collections
2527

28+
`FromCollection` allows a collection of Eloquent models to be returned. Every Model will become a row in the export.
29+
2630
```php
2731
namespace App\Exports;
2832

2933
use App\Invoice;
3034
use Maatwebsite\Excel\Concerns\FromCollection;
3135

32-
class InvoicesExport implements FromCollection
36+
class UsersExport implements FromCollection
3337
{
3438
public function collection()
3539
{
36-
return Invoice::all();
40+
// All User models.
41+
return User::all();
42+
43+
// Query some User models.
44+
return User::query()->select('')->where('country', 'NL')->get();
3745
}
3846
}
3947
```
4048

49+
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).
50+
51+
```php
52+
class UsersExport implements FromCollection
53+
{
54+
public function __construct(
55+
public string $country
56+
) {
57+
}
58+
59+
public function collection()
60+
{
61+
return User::query()
62+
->join('orders', 'users.id', '=', 'orders.user_id')
63+
->select('users.name', 'users.country', 'orders.price')
64+
->where('users.country', $this->country)
65+
->get();
66+
}
67+
}
68+
```
69+
70+
In the controller you can pass any required data to the export.
71+
72+
```php
73+
return Excel::download(new UsersExport('NL'));
74+
```
75+
4176
### Custom collections
4277

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

4581
```php
4682
class InvoicesExport implements FromCollection
@@ -96,9 +132,11 @@ class InvoicesExport implements FromArray
96132

97133
## Query
98134

99-
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.
135+
In the previous examples, we did the query inside the export class. While this is a good solution for small exports, for
136+
bigger exports this will come at a hefty performance price.
100137

101-
By using the `FromQuery` concern, we can prepare a query for an export. Behind the scenes this query is executed in chunks.
138+
By using the `FromQuery` concern, we can prepare a query for an export. Behind the scenes this query is executed in
139+
chunks to limit the amount of models held in memory.
102140

103141
In the `InvoicesExport` class, add the `FromQuery` concern and return a query. Be sure to not `->get()` the results!
104142

@@ -154,10 +192,10 @@ It will convert an HTML table into an Excel spreadsheet. For example; `exports/u
154192
</thead>
155193
<tbody>
156194
@foreach($users as $user)
157-
<tr>
158-
<td>{{ $user->name }}</td>
159-
<td>{{ $user->email }}</td>
160-
</tr>
195+
<tr>
196+
<td>{{ $user->name }}</td>
197+
<td>{{ $user->email }}</td>
198+
</tr>
161199
@endforeach
162200
</tbody>
163201
</table>
@@ -167,7 +205,8 @@ It will convert an HTML table into an Excel spreadsheet. For example; `exports/u
167205

168206
Exports can be created from a PHP generator class, by using the `FromGenerator` concern.
169207

170-
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.
208+
A generator allows you to write code that uses foreach to iterate over a set of data without needing to build an array
209+
in memory.
171210

172211
```php
173212
namespace App\Exports;
@@ -186,3 +225,35 @@ class DataExport implements FromGenerator
186225
}
187226
}
188227
```
228+
229+
## Generic data manipulations
230+
231+
### Strict null comparison
232+
233+
If you want your `0` values to be actual `0` values in your Excel sheet instead of `null` (empty cells), you can use `WithStrictNullComparison`.
234+
235+
```php
236+
use Maatwebsite\Excel\Concerns\WithStrictNullComparison;
237+
238+
class UsersExport implements FromCollection, WithStrictNullComparison
239+
```
240+
241+
### Custom start cell
242+
243+
The default start cell is A1. Implementing the `WithCustomStartCell` concern in your export class allows you to specify a custom start cell.
244+
245+
```php
246+
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
247+
248+
class UsersExport implements FromCollection, WithCustomStartCell
249+
{
250+
public function startCell(): string
251+
{
252+
return 'B2';
253+
}
254+
}
255+
```
256+
257+
:::warning
258+
WithCustomStartCell is only supported for FromCollection exports.
259+
:::

4.x/exports/exporting.md

+48
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,54 @@ Instead of using the facade, you can now chain methods like `download` and `stor
103103
return (new UsersExport)->download('users.xlsx');
104104
```
105105

106+
### Responsables
107+
108+
The previous (download) example can be made even shorter when adding Laravel's `Responsable` interface to the export class:
109+
110+
```php
111+
namespace App\Exports;
112+
113+
use App\User;
114+
use Maatwebsite\Excel\Excel;
115+
use Illuminate\Contracts\Support\Responsable;
116+
use Maatwebsite\Excel\Concerns\FromCollection;
117+
use Maatwebsite\Excel\Concerns\Exportable;
118+
119+
class UsersExport implements FromCollection, Responsable
120+
{
121+
use Exportable;
122+
123+
/**
124+
* It's required to define the fileName within
125+
* the export class when making use of Responsable.
126+
*/
127+
private $fileName = 'users.xlsx';
128+
129+
/**
130+
* Optional Writer Type
131+
*/
132+
private $writerType = Excel::XLSX;
133+
134+
/**
135+
* Optional headers
136+
*/
137+
private $headers = [
138+
'Content-Type' => 'text/csv',
139+
];
140+
141+
public function collection()
142+
{
143+
return User::all();
144+
}
145+
}
146+
```
147+
148+
Within your controller you can now, just return the Export class and it will internally trigger the download response.
149+
150+
```php
151+
return new UsersExport();
152+
```
153+
106154
### Dependency injection
107155

108156
```php

4.x/exports/performance.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Performance
2+
3+
[[toc]]
4+
5+
## TLDR;
6+
7+
<span class="inline-step">1</span> ****
8+
9+
## Limiting data
10+
11+
## Limiting query size
12+
13+
## Using LazyCollections/Generators
14+
15+
## Cell caching

4.x/exports/presentation.md

+58-14
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ In case of using the Eloquent query builder:
6363
use Maatwebsite\Excel\Concerns\FromQuery;
6464
use Maatwebsite\Excel\Concerns\WithMapping;
6565

66-
class InvoicesExport implements FromQuery, WithMapping
66+
class UsersExport implements FromQuery, WithMapping
6767
{
6868
/**
69-
* @var Invoice $invoice
69+
* @var User $user
7070
*/
71-
public function map($invoice): array
71+
public function map($user): array
7272
{
7373
return [
74-
$invoice->invoice_number,
75-
$invoice->user->name,
76-
Date::dateTimeToExcel($invoice->created_at),
74+
$user->id,
75+
$user->lastOrder->description,
76+
Date::dateTimeToExcel($user->created_at),
7777
];
7878
}
7979
}
@@ -84,29 +84,71 @@ class InvoicesExport implements FromQuery, WithMapping
8484
You can also return multiple rows inside the map function.
8585

8686
```php
87-
public function map($invoice): array
87+
public function map($user): array
8888
{
8989
// This example will return 3 rows.
9090
// First row will have 2 column, the next 2 will have 1 column
9191
return [
9292
[
93-
$invoice->invoice_number,
94-
Date::dateTimeToExcel($invoice->created_at),
93+
$user->id,
94+
Date::dateTimeToExcel($user->created_at),
9595
],
9696
[
97-
$invoice->lines->first()->description,
97+
$user->orders->first()->description,
9898
],
9999
[
100-
$invoice->lines->last()->description,
100+
$user->orders->last()->description,
101101
]
102102
];
103103
}
104104
```
105105

106+
## Heading row
107+
108+
If you are using columns, the heading row will already be applied by using the name of the column.
109+
110+
```php
111+
use Maatwebsite\Excel\Concerns\WithColumns;
112+
113+
class UsersExport implements WithColumns
114+
{
115+
public function columns(): array
116+
{
117+
return [
118+
Number::make('#', 'id'),
119+
Text::make('Name'),
120+
Text::make('Email'),
121+
Text::make('Registration Date'),
122+
];
123+
}
124+
}
125+
```
126+
127+
You can also add a heading row by adding the `WithHeadings` concern. The heading row will be added as very first row of the sheet.
128+
129+
```php
130+
use Maatwebsite\Excel\Concerns\WithHeadings;
131+
132+
class UsersExport implements WithHeadings
133+
{
134+
public function headings(): array
135+
{
136+
return [
137+
'#',
138+
'Name',
139+
'Email',
140+
'Date',
141+
];
142+
}
143+
}
144+
```
145+
106146
## Styling
107147

108148
### Column styling
109149

150+
Styles can be applied on a per-column basis. Styles can be passed as a PhpSpreadsheet style array via the `style()` method.
151+
110152
```php
111153
Text::make('Name')->style([
112154
'font' => [
@@ -115,6 +157,8 @@ Text::make('Name')->style([
115157
]);
116158
```
117159

160+
Styles can also be applied by using the Fluent syntax.
161+
118162
```php
119163
Text::make('Name')
120164
->font('Calibri', 16.0)
@@ -143,7 +187,7 @@ namespace App\Exports;
143187
use Maatwebsite\Excel\Concerns\WithStyles;
144188
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
145189

146-
class InvoicesExport implements WithStyles
190+
class UsersExport implements WithStyles
147191
{
148192
public function styles(Worksheet $sheet)
149193
{
@@ -171,7 +215,7 @@ namespace App\Exports;
171215
use Maatwebsite\Excel\Concerns\WithStyles;
172216
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
173217

174-
class InvoicesExport implements WithStyles
218+
class UsersExport implements WithStyles
175219
{
176220
public function styles(Worksheet $sheet)
177221
{
@@ -198,7 +242,7 @@ namespace App\Exports;
198242

199243
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
200244

201-
class InvoicesExport implements ShouldAutoSize
245+
class UsersExport implements ShouldAutoSize
202246
{
203247
...
204248
}

4.x/exports/testing.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Testing

0 commit comments

Comments
 (0)