|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Imports; |
| 4 | + |
| 5 | +use App\Rules\StrongPassword; |
| 6 | +use App\User; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | +use Illuminate\Support\Facades\Hash; |
| 9 | +use Illuminate\Support\Facades\Log; |
| 10 | +use Maatwebsite\Excel\Concerns\Importable; |
| 11 | +use Maatwebsite\Excel\Concerns\SkipsErrors; |
| 12 | +use Maatwebsite\Excel\Concerns\SkipsFailures; |
| 13 | +use Maatwebsite\Excel\Concerns\SkipsOnError; |
| 14 | +use Maatwebsite\Excel\Concerns\SkipsOnFailure; |
| 15 | +use Maatwebsite\Excel\Concerns\ToModel; |
| 16 | +use Maatwebsite\Excel\Concerns\WithBatchInserts; |
| 17 | +use Maatwebsite\Excel\Concerns\WithChunkReading; |
| 18 | +use Maatwebsite\Excel\Concerns\WithValidation; |
| 19 | +use Maatwebsite\Excel\Concerns\WithHeadingRow; |
| 20 | + |
| 21 | +class UsersImport implements ToModel, WithBatchInserts, WithChunkReading, WithValidation, WithHeadingRow, SkipsOnFailure, SkipsOnError |
| 22 | +{ |
| 23 | + use Importable, SkipsFailures, SkipsErrors; |
| 24 | + |
| 25 | + /** |
| 26 | + * @param array $row |
| 27 | + * @return Model|null |
| 28 | + */ |
| 29 | + public function model(array $row) |
| 30 | + { |
| 31 | + return new User([ |
| 32 | + 'first_name' => $row['first_name'], |
| 33 | + 'last_name' => $row['last_name'], |
| 34 | + 'organization_name' => $row['organization_name'], |
| 35 | + 'job_title' => $row['job_title'], |
| 36 | + 'email' => $row['email'], |
| 37 | + 'password' => Hash::make($row['password']), |
| 38 | + ]); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @return int |
| 43 | + */ |
| 44 | + public function batchSize(): int |
| 45 | + { |
| 46 | + return 1000; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return int |
| 51 | + */ |
| 52 | + public function chunkSize(): int |
| 53 | + { |
| 54 | + return 1000; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return array |
| 59 | + */ |
| 60 | + public function rules(): array |
| 61 | + { |
| 62 | + return [ |
| 63 | + '*.first_name' => 'required|string|max:255', |
| 64 | + '*.last_name' => 'required|string|max:255', |
| 65 | + '*.organization_name' => 'max:255', |
| 66 | + '*.job_title' => 'max:255', |
| 67 | + '*.email' => 'required|email|max:255|unique:users,email', |
| 68 | + '*.password' => ['required', 'string', new StrongPassword], |
| 69 | + ]; |
| 70 | + } |
| 71 | +} |
0 commit comments