PhoneImport.php 769 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Imports;
  3. use App\Model\PhoneInfo;
  4. use Maatwebsite\Excel\Concerns\ToModel;
  5. use Maatwebsite\Excel\Concerns\WithStartRow;
  6. use Maatwebsite\Excel\Concerns\Importable;
  7. use Maatwebsite\Excel\Concerns\WithValidation;
  8. class PhoneImport implements ToModel,WithStartRow,WithValidation
  9. {
  10. use Importable;
  11. /**
  12. * @param array $row
  13. *
  14. * @return \Illuminate\Database\Eloquent\Model|null
  15. */
  16. public function model(array $row)
  17. {
  18. return new PhoneInfo([
  19. 'phone' => $row[0]
  20. ]);
  21. }
  22. // 从2行开始读取数据
  23. public function startRow(): int
  24. {
  25. return 2;
  26. }
  27. // 验证
  28. public function rules(): array
  29. {
  30. return [
  31. '0' => 'required',
  32. ];
  33. }
  34. }