🚀 5 minutes quick start
💪 Create an import class in app/Imports
You may do this by using the make:import
command.
php artisan make:import UsersImport --model=User
1
The file can be found in app/Imports
:
.
├── app
│ ├── Imports
│ │ ├── UsersImport.php
│
└── composer.json
If you prefer to create the import manually, you can create the following in app/Imports
:
<?php
namespace App\Imports;
use App\User;
use Illuminate\Support\Facades\Hash;
use Nikazooz\Simplesheet\Concerns\ToModel;
class UsersImport implements ToModel
{
/**
* @param array $row
* @return User|null
*/
public function model(array $row)
{
return new User([
'name' => $row[0],
'email' => $row[1],
'password' => Hash::make($row[2]),
]);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
🔥 In your controller you can call this import now:
use App\Http\Controllers\Controller;
use App\Imports\UsersImport;
use Nikazooz\Simplesheet\Facades\Simplesheet;
class UsersController extends Controller
{
public function import()
{
Simplesheet::import(new UsersImport, 'users.xlsx');
return redirect('/')->with('success', 'All good!');
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
📄 Find the imported users in your database!