Skip to content

Commit 8cf9e10

Browse files
Add importing basics
1 parent 0a3141a commit 8cf9e10

File tree

5 files changed

+219
-135
lines changed

5 files changed

+219
-135
lines changed

.vuepress/3.1.js

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module.exports = [
3636
collapsable: false,
3737
children: prefix('imports', [
3838
'',
39+
'basics',
3940
'collection',
4041
'model',
4142
'importables',

3.1/imports/README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ If you prefer to create the import manually, you can create the following in `Ap
1616
namespace App\Imports;
1717

1818
use App\User;
19+
use Illuminate\Support\Facades\Hash;
1920
use Maatwebsite\Excel\Concerns\ToModel;
2021

2122
class UsersImport implements ToModel
@@ -28,8 +29,9 @@ class UsersImport implements ToModel
2829
public function model(array $row)
2930
{
3031
return new User([
31-
'name' => $row[0],
32-
'email' => $row[1],
32+
'name' => $row[0],
33+
'email' => $row[1],
34+
'password' => Hash::make($row[2]),
3335
]);
3436
}
3537
}
@@ -49,7 +51,7 @@ class UsersController extends Controller
4951
{
5052
Excel::import(new UsersImport, 'users.xlsx');
5153

52-
return 'All good!';
54+
return back()->with('success', ''All good!');
5355
}
5456
}
5557
```

3.1/imports/basics.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
## Importing basics
2+
3+
If you have followed the 5 Minute Quickstart, you'll already have a `UsersImport` class.
4+
5+
```php
6+
<?php
7+
8+
namespace App\Imports;
9+
10+
use App\User;
11+
use Illuminate\Support\Facades\Hash;
12+
use Maatwebsite\Excel\Concerns\ToModel;
13+
14+
class UsersImport implements ToModel
15+
{
16+
/**
17+
* @param array $row
18+
*
19+
* @return User|null
20+
*/
21+
public function model(array $row)
22+
{
23+
return new User([
24+
'name' => $row[0],
25+
'email' => $row[1],
26+
'password' => Hash::make($row[2]),
27+
]);
28+
}
29+
}
30+
```
31+
32+
### Importing from default disk
33+
34+
Passing the UsersImport object to the Excel::import() method, will tell the package how to import the file that is passed as second parameter.
35+
The file is expected to be located in your default filesystem disk (see config/filesystems.php).
36+
37+
```php
38+
Excel::import(new UsersImport, 'users.xlsx');
39+
```
40+
41+
### Importing from anther disk
42+
43+
You can specify another disk with the third parameter like your Amazon s3 disk. (see config/filesystems.php)
44+
45+
```php
46+
Excel::import(new UsersImport, 'users.xlsx', 's3');
47+
```
48+
49+
### Importing uploaded files
50+
51+
If you let your user upload the document, you can also just pass the uploaded file directly.
52+
53+
```php
54+
Excel::import(new UsersImport, request()->file('your_file'));
55+
```
56+
57+
### Importing to array or collection
58+
59+
If you want to bypass the `ToArray` or `ToCollection` concerns and want to have an array of imported data in your controller (beware of performance!), you can use the `::toArray()` or `::toCollection()` method.
60+
61+
```php
62+
$array = Excel::toArray(new UsersImport, 'users.xlsx');
63+
64+
$collection = Excel::toCollection(new UsersImport, 'users.xlsx');
65+
```
66+
67+
### Specifying a reader type
68+
69+
If the reader type is not detectable by the file extension, you can specify a reader type by passing it as fourth parameter.
70+
71+
```php
72+
Excel::import(new UsersImport, 'users.xlsx', 's3', \Maatwebsite\Excel\Excel::XLSX);
73+
```

3.1/imports/importables.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,19 @@ class UsersImport implements ToModel
2727
We can now import without the need for the facade:
2828

2929
```php
30-
return (new UsersImport)->import('users.xlsx');
30+
(new UsersImport)->import('users.xlsx', 'local', \Maatwebsite\Excel\Excel::XLSX);
3131
```
3232

3333
Or queue the import:
3434

3535
```php
36-
return (new UsersImport)->queue('users.xlsx');
36+
(new UsersImport)->queue('users.xlsx');
3737
```
38+
39+
The import can be loaded into an array or collection:
40+
41+
```php
42+
$array = (new UsersImport)->toArray('users.xlsx');
43+
44+
$collection = (new UsersImport)->toCollection('users.xlsx');
45+
```

0 commit comments

Comments
 (0)