1//Install this package https://docs.laravel-excel.com/3.1/getting-started/installation.html
2php artisan make:import UsersImport --model=User
3<?php
4namespace App\Imports;
5
6use App\User;
7use Illuminate\Support\Facades\Hash;
8use Maatwebsite\Excel\Concerns\Importable;
9use Maatwebsite\Excel\Concerns\ToModel;
10use Maatwebsite\Excel\Concerns\WithProgressBar;
11
12class UsersImport implements ToModel, WithProgressBar
13{
14 use Importable;
15
16 public function model(array $row)
17 {
18 return new User([
19 'name' => $row[0],
20 'email' => $row[1],
21 'password' => Hash::make($row[2]),
22 ]);
23 }
24}
25
26//In your console command, you'd use it as follows:
27
28<?php
29
30namespace App\Console\Commands;
31
32use App\Imports\UsersImport;
33use Illuminate\Console\Command;
34
35class ImportExcel extends Command
36{
37 protected $signature = 'import:excel';
38
39 protected $description = 'Laravel Excel importer';
40
41 public function handle()
42 {
43 $this->output->title('Starting import');
44 (new UsersImport)->withOutput($this->output)->import('users.xlsx');
45 $this->output->success('Import successful');
46 }
47}
48
49// finally run this command
50php artisan import:excel